Pipes Introduction
Learn how Angular Pipes transform and format data directly inside templates without changing the original component data.
What are Pipes?
Pipes are Angular features used to transform and format data before displaying it in the template.
They are useful for formatting dates, numbers, currency, text, and other values in a readable way.
Pipe Flow
Component Data
Data is defined inside the component.
Pipe
The pipe transforms the value.
Template
The transformed value is displayed.
UI
The formatted data appears to the user.
Basic Pipe Syntax
{{value | pipeName}}
{{ "SAM" | lowercase }}
Common Angular Pipes
UpperCase
Converts text into uppercase characters.
uppercase
LowerCase
Converts text into lowercase characters.
lowercase
Date
Formats date values for display.
date
Currency
Formats numbers as currency values.
currency
Uppercase Pipe
The uppercase pipe converts a string into
uppercase characters.
studentName = 'Samiksha';
<p>
{{studentName | uppercase}}
</p>
Lowercase Pipe
The lowercase pipe converts a string into
lowercase characters.
<p>
{{studentName | lowercase}}
</p>
Date Pipe
The date pipe formats date values into
readable formats.
joiningDate = new Date();
<p>
{{joiningDate | date}}
</p>
Currency Pipe
The currency pipe formats a number as a
currency value.
courseFee = 35000;
<p>
{{courseFee | currency:'INR'}}
</p>
Pipe with Parameters
Some pipes accept parameters to control how the value should be formatted.
{{courseFee | currency:'INR'}}
'INR' is passed as a parameter to the
currency pipe.
Using Multiple Pipes
Multiple pipes can be chained together when more than one transformation is required.
{{studentName | uppercase}}
Student Data Examples
student1 = 'Sam';
student2 = 'Pradnya';
student3 = 'Samiksha';
courseFee = 35000;
joiningDate = new Date();
<p>{{student1 | uppercase}}</p>
<p>{{student2 | lowercase}}</p>
<p>{{student3 | uppercase}}</p>
<p>{{courseFee | currency:'INR'}}</p>
<p>{{joiningDate | date}}</p>
Student Information
Sam
Pradnya
Samiksha
Course Fee: ₹35,000.00
Benefits of Pipes
Formatting
Display data in a user-friendly format.
Clean Templates
Keep common formatting logic simple.
Reusable
Built-in pipes can be reused across templates.
Readable Data
Convert raw values into readable UI output.
Common Pipe Reference
| Pipe | Purpose | Example |
|---|---|---|
uppercase |
Convert text to uppercase | {{name | uppercase}} |
lowercase |
Convert text to lowercase | {{name | lowercase}} |
date |
Format date | {{date | date}} |
currency |
Format currency | {{fee | currency:'INR'}} |
Best Practices
- Use pipes mainly for presentation and formatting.
- Keep complex business logic outside the template.
- Use meaningful pipe parameters.
- Use built-in pipes whenever they satisfy the requirement.
- Create custom pipes when reusable transformation logic is required.
- Avoid unnecessary pipe chains in complex templates.
Summary
- Pipes transform and format data in Angular templates.
- The pipe operator is represented using
|. uppercaseconverts text to uppercase.lowercaseconverts text to lowercase.dateformats date values.currencyformats currency values.- Pipes can accept parameters for customized formatting.