Custom Pipes
Learn how to create reusable Angular Pipes for custom data transformation and formatting requirements.
What are Custom Pipes?
Custom Pipes are developer-created Pipes used when the built-in Angular Pipes do not provide the required transformation.
They allow reusable formatting or transformation logic to be used directly inside Angular templates.
Custom Pipe Flow
Input Data
Component provides the original value.
Custom Pipe
The Pipe receives the value.
Transform
The custom transformation is performed.
Output
The transformed value is displayed.
Why Use Custom Pipes?
Reusable
Use the same transformation in multiple templates.
Clean Templates
Keep transformation logic outside complex template expressions.
Flexible
Create formatting rules specific to your application.
Maintainable
Keep reusable transformation logic in one place.
Creating a Custom Pipe
Angular CLI can generate a custom Pipe automatically.
ng generate pipe studentName
Short form:
ng g pipe studentName
Custom Pipe Structure
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'studentName'
})
export class StudentNamePipe implements PipeTransform {
transform(value: string): string {
return value;
}
}
@Pipe decorator defines the Pipe and
PipeTransform provides the transformation
method.
The transform() Method
The transform() method receives the input value
and returns the transformed result.
transform(value: string): string {
return value.toUpperCase();
}
Complete Custom Pipe Example
import {
Pipe,
PipeTransform
} from '@angular/core';
@Pipe({
name: 'studentName'
})
export class StudentNamePipe implements PipeTransform {
transform(value: string): string {
return value.toUpperCase();
}
}
studentName = 'Samiksha';
<p>
{{studentName | studentName}}
</p>
Custom Pipe with Parameter
Custom Pipes can also accept parameters to make the transformation configurable.
transform(
value: string,
prefix: string
): string {
return prefix + value;
}
<p>
{{studentName | studentName:'Student: '}}
</p>
Using a Custom Pipe with Multiple Students
student1 = 'Sam';
student2 = 'Pradnya';
student3 = 'Samiksha';
<p>
{{student1 | studentName}}
</p>
<p>
{{student2 | studentName}}
</p>
<p>
{{student3 | studentName}}
</p>
SAM
PRADNYA
SAMIKSHA
Built-in Pipe vs Custom Pipe
| Built-in Pipe | Custom Pipe |
|---|---|
| Provided by Angular | Created by developer |
| Ready to use | Created for specific requirements |
| Examples: date, currency, uppercase | Example: studentName |
| General-purpose transformations | Application-specific transformations |
Common Custom Pipe Use Cases
Name Formatting
Format student or employee names consistently.
Custom Formatting
Create application-specific display formats.
Text Transformation
Transform text according to application requirements.
Reusable Logic
Reuse the same transformation in multiple views.
Custom Pipe Transformation Flow
Best Practices
- Create a custom Pipe only when built-in Pipes are not sufficient.
- Keep the transformation logic simple and focused.
- Use meaningful Pipe names.
- Keep reusable formatting logic inside the Pipe.
- Use parameters when the transformation needs customization.
- Avoid placing complex business logic inside Pipes.
- Keep Pipes focused on presentation and transformation.
Summary
- Custom Pipes are created for application-specific transformations.
- The
@Pipedecorator defines a custom Pipe. PipeTransformprovides thetransform()method.- The
transform()method receives and transforms data. - Custom Pipes can accept parameters.
- Custom Pipes make reusable transformation logic easier to maintain.
- Use built-in Pipes whenever they already satisfy the requirement.