Chaining Pipes
Learn how Angular allows multiple Pipes to be combined together to transform and format data step by step.
What is Pipe Chaining?
Pipe chaining means using more than one Pipe in the same Angular template expression.
The output of one Pipe becomes the input for the next Pipe, allowing multiple transformations to be performed sequentially.
Pipe Chaining Flow
Original Data
Component provides the original value.
First Pipe
The first transformation is applied.
Second Pipe
The transformed value passes to another Pipe.
Final Output
The final formatted value is displayed.
Basic Chaining Syntax
{{value | pipeOne | pipeTwo}}
Example 1: Uppercase and Slice
We can first convert a name to uppercase and then display only a portion of the result.
studentName = 'Samiksha';
<p>
{{studentName | uppercase | slice:0:5}}
</p>
Example 2: Lowercase and Slice
studentName = 'PRADNYA';
<p>
{{studentName | lowercase | slice:0:4}}
</p>
Example 3: Date Formatting
Date formatting can also be customized using a Pipe parameter.
joiningDate = new Date();
<p>
{{joiningDate | date:'dd/MM/yyyy'}}
</p>
Using Multiple Pipes
More than two Pipes can also be combined when the output requires multiple transformations.
{{studentName | uppercase | slice:0:6}}
Chaining Pipes with Parameters
Pipes in a chain can also receive their own parameters.
{{studentName | uppercase | slice:0:5}}
uppercase performs the first transformation,
and slice receives its own parameters.
Single Pipe vs Chained Pipes
| Type | Syntax | Purpose |
|---|---|---|
| Single Pipe | {{name | uppercase}} |
One transformation |
| Two Pipes | {{name | uppercase | slice:0:5}} |
Two transformations |
| Parameterized Pipe | {{date | date:'dd/MM/yyyy'}} |
Customized formatting |
Complete Student Example
export class StudentComponent {
student1 = 'Sam';
student2 = 'Pradnya';
student3 = 'Samiksha';
joiningDate = new Date();
}
<h3>
{{student1 | uppercase | slice:0:2}}
</h3>
<h3>
{{student2 | uppercase | slice:0:5}}
</h3>
<h3>
{{student3 | uppercase | slice:0:6}}
</h3>
<p>
{{joiningDate | date:'dd/MM/yyyy'}}
</p>
SA
PRADN
SAMIKS
Date displayed using the Date Pipe.
Chaining Example Flow
Best Practices
- Use pipe chaining when multiple simple transformations are required.
- Keep the chain short and readable.
- Use parameters only when they are required.
- Avoid very long and complex pipe chains.
- Keep business logic outside template expressions.
- Use custom Pipes when transformation logic becomes reusable and complex.
Summary
- Pipe chaining combines multiple Pipes in one expression.
- The output of one Pipe becomes the input of the next Pipe.
- Pipes are applied from left to right.
- Each Pipe can have its own parameters.
- Chaining is useful for simple sequential transformations.
- Very complex chains should be avoided for better readability.