Angular provides a powerful feature called pipes, which allow developers to transform data directly in templates. Pipes are simple yet powerful tools to format, transform, and display data in a way that is user-friendly and consistent. Whether it’s formatting dates, converting text to uppercase, or creating custom transformations, Angular pipes help streamline development.
Table of Contents
- Introduction to Angular Pipes
- Built-in Angular Pipes
- Text Transformation Pipes
- Date and Time Pipes
- Number and Currency Pipes
- Other Useful Built-in Pipes
- Custom Pipes
- Creating a Custom Pipe
- Example: Reverse String Pipe
- Using Pipes in Templates
- Chaining Pipes
- Parameterized Pipes
- Performance Considerations
- Best Practices
- Common Mistakes
- Practical Examples
- Conclusion
1. Introduction to Angular Pipes
A pipe in Angular is a way to transform displayed data in the template without modifying the underlying data in the component. Pipes are represented by the |
symbol in Angular templates.
Example:
<p>{{ name | uppercase }}</p>
In this example, the name
variable is transformed to uppercase when displayed in the template, but the original name
value in the component remains unchanged.
Benefits of Pipes
- Simplifies template logic
- Avoids repetitive code in components
- Provides clean, readable templates
- Supports built-in and custom transformations
2. Built-in Angular Pipes
Angular provides several built-in pipes for common transformations, including text formatting, dates, numbers, and currency.
2.1 Text Transformation Pipes
- uppercase: Converts text to uppercase.
- lowercase: Converts text to lowercase.
- titlecase: Converts text to title case.
Example:
// app.component.ts
export class AppComponent {
name: string = 'angular pipes';
}
<p>Original: {{ name }}</p>
<p>Uppercase: {{ name | uppercase }}</p>
<p>Lowercase: {{ name | lowercase }}</p>
<p>Title Case: {{ name | titlecase }}</p>
Output:
- Original: angular pipes
- Uppercase: ANGULAR PIPES
- Lowercase: angular pipes
- Title Case: Angular Pipes
2.2 Date and Time Pipes
Angular provides a date
pipe to format JavaScript Date
objects.
Syntax:
{{ dateValue | date:'format' }}
Common formats:
'shortDate'
→ MM/dd/yyyy'mediumDate'
→ MMM d, y'fullDate'
→ EEEE, MMMM d, yyyy'shortTime'
→ h:mm a
Example:
export class AppComponent {
birthday: Date = new Date(1990, 4, 15);
}
<p>Short Date: {{ birthday | date:'shortDate' }}</p>
<p>Medium Date: {{ birthday | date:'mediumDate' }}</p>
<p>Full Date: {{ birthday | date:'fullDate' }}</p>
<p>Short Time: {{ birthday | date:'shortTime' }}</p>
Output:
- Short Date: 05/15/1990
- Medium Date: May 15, 1990
- Full Date: Tuesday, May 15, 1990
- Short Time: 12:00 AM
2.3 Number and Currency Pipes
Angular provides number
and currency
pipes to format numeric data.
Number Pipe
<p>{{ 1234.567 | number:'1.2-2' }}</p>
1.2-2
means:- Minimum integer digits: 1
- Minimum fraction digits: 2
- Maximum fraction digits: 2
Output: 1,234.57
Currency Pipe
export class AppComponent {
price: number = 2500;
}
<p>Price: {{ price | currency:'USD':'symbol' }}</p>
<p>Price in INR: {{ price | currency:'INR':'symbol' }}</p>
Output:
- Price: $2,500.00
- Price in INR: ₹2,500.00
2.4 Other Useful Built-in Pipes
- percent: Converts a number to percentage.
<p>{{ 0.25 | percent }}</p>
Output: 25%
- json: Converts an object into JSON string.
export class AppComponent {
person = { name: 'John', age: 30 };
}
<p>{{ person | json }}</p>
Output: {"name":"John","age":30}
- slice: Extracts a portion of an array or string.
<p>{{ 'Angular Pipes' | slice:0:7 }}</p>
Output: Angular
3. Custom Pipes
While built-in pipes cover most use cases, custom pipes allow developers to implement specific transformations.
3.1 Creating a Custom Pipe
To create a custom pipe, use the @Pipe
decorator and implement the PipeTransform
interface.
Syntax:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'pipeName' })
export class CustomPipe implements PipeTransform {
transform(value: any, ...args: any[]): any {
// transformation logic here
return transformedValue;
}
}
3.2 Example: Reverse String Pipe
A common example is reversing a string.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'reverse' })
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
Use in template:
export class AppComponent {
name: string = 'Angular';
}
<p>Original: {{ name }}</p>
<p>Reversed: {{ name | reverse }}</p>
Output:
- Original: Angular
- Reversed: ralugnA
4. Using Pipes in Templates
Pipes are easy to use in templates. Simply add the |
symbol after a variable and provide the pipe name.
<p>{{ birthday | date:'fullDate' }}</p>
<p>{{ amount | currency:'USD':'symbol' }}</p>
<p>{{ text | uppercase }}</p>
Pipes can also accept arguments for additional configuration.
5. Chaining Pipes
Angular allows chaining multiple pipes in a single template expression.
Example:
<p>{{ name | uppercase | slice:0:4 }}</p>
If name = 'Angular Pipes'
, the output will be: ANGU
6. Parameterized Pipes
Some pipes accept parameters to customize output. For example:
<p>{{ birthday | date:'dd/MM/yyyy' }}</p>
<p>{{ price | currency:'EUR':'code':'1.2-2' }}</p>
- Date format:
'dd/MM/yyyy'
- Currency: specify currency code, display format, and digits
Custom pipes can also accept multiple parameters:
@Pipe({ name: 'exclaim' })
export class ExclaimPipe implements PipeTransform {
transform(value: string, symbol: string, times: number): string {
return value + symbol.repeat(times);
}
}
<p>{{ 'Hello' | exclaim:'!':3 }}</p>
Output: Hello!!!
7. Performance Considerations
- Pure vs Impure Pipes: By default, Angular pipes are pure (stateless). They execute only when input changes.
- Impure pipes execute on every change detection cycle, which may affect performance.
@Pipe({ name: 'impurePipe', pure: false })
- Avoid heavy calculations inside pipes; use them only for presentation logic.
8. Best Practices
- Keep pipes pure unless absolutely necessary.
- Avoid logic-heavy operations inside pipes.
- Use pipes for display purposes, not for data transformations that affect business logic.
- Chain pipes for clean and readable templates.
- Use custom pipes for repeated transformation logic.
9. Common Mistakes
- Forgetting to declare the pipe in
app.module.ts
. - Using impure pipes unnecessarily, affecting performance.
- Trying to modify the original value in a pipe (pipes should not mutate input).
- Overusing pipes in complex expressions, which reduces readability.
10. Practical Examples
Example 1: Display User List with Pipes
export class AppComponent {
users = [
{ name: 'Alice', birthday: new Date(1992, 5, 21) },
{ name: 'Bob', birthday: new Date(1988, 10, 10) }
];
}
<ul>
<li *ngFor="let user of users">
Name: {{ user.name | uppercase }}, Birthday: {{ user.birthday | date:'shortDate' }}
</li>
</ul>
Output:
- Name: ALICE, Birthday: 06/21/1992
- Name: BOB, Birthday: 11/10/1988
Example 2: Custom Reverse Pipe
<p>Reversed Name: {{ 'Angular' | reverse }}</p>
Output: ralugnA
Example 3: Chained Pipes
<p>{{ 'hello world' | uppercase | slice:0:5 }}</p>
Output: HELLO
Leave a Reply