Explain it simply
Custom Pipes, in plain language
You can create custom pipes for your application's specific requirements.
Problem → Why → Solution
The problem — why this concept matters
Filtering lists with an impure pipe can rerun on every change-detection pass and harm performance.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
Use a custom pipe for deterministic presentation-only transformation, not business filtering or side effects. A pure pipe should produce the same result for the same arguments; expensive computation may belong in the data model or computed state. Define its name, input and output types, null behavior, and locale assumptions as a public API.
- input and state
- component and template
- rendered Angular UI
Explain the core concept and Angular data flow of Custom Pipes
Core syntax or pattern
// reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse'
})
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
// exponential.pipe.ts
@Pipe({
name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
transform(value: number, exponent: number = 1): number {
return Math.pow(value, exponent);
}
}
// Usage in template
<p>{{ 'Angular' | reverse }}</p> <!-- ralugnA -->
<p>{{ 2 | exponential:3 }}</p> <!-- 8 (2^3) -->
<p>{{ 5 | exponential:2 }}</p> <!-- 25 (5^2) -->
// Don't forget to declare pipes in module
@NgModule({
declarations: [ReversePipe, ExponentialPipe]
})Use a custom pipe for deterministic presentation-only transformation, not business filtering or side effects. A pure pipe should produce the same result for the same arguments; expensive computation may belong in the data model or computed state. Define its name, input and output types, null behavior, and locale assumptions as a public API.
Read the code step by step
- input and state
// reverse.pipe.tsYou can create custom pipes for your application's specific requirements.
- component and template
})Use a custom pipe for deterministic presentation-only transformation, not business filtering or side effects. A pure pipe should produce the same result for the same arguments; expensive computation may belong in the data model or computed state. Define its name, input and output types, null behavior, and locale assumptions as a public API.
- rendered Angular UI
})Create a file-size formatting pipe and unit-test bytes, KB, MB, zero, negative, and null values.
- input and state
- component and template
- rendered Angular UI
Success
Explain the core concept and Angular data flow of Custom Pipes
Failure
Filtering lists with an impure pipe can rerun on every change-detection pass and harm performance.
You can create custom pipes for your application's specific requirements.
// reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse'
})
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
// exponential.pipe.ts
@Pipe({
name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
transform(value: number, exponent: number = 1): number {
return Math.pow(value, exponent);
}
}
// Usage in template
<p>{{ 'Angular' | reverse }}</p> <!-- ralugnA -->
<p>{{ 2 | exponential:3 }}</p> <!-- 8 (2^3) -->
<p>{{ 5 | exponential:2 }}</p> <!-- 25 (5^2) -->
// Don't forget to declare pipes in module
@NgModule({
declarations: [ReversePipe, ExponentialPipe]
})(The custom pipes work and transform the data)Real-world usage
How to apply it in a real project
Create a file-size formatting pipe and unit-test bytes, KB, MB, zero, negative, and null values.
Mini challenge
Build it yourself
Rebuild the Custom Pipes example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse'
})
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
// exponential.pipe.ts
@Pipe({
name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
transform(value: number, exponent: number = 1): number {
return Math.pow(value, exponent);
}
}
// Usage in template
<p>{{ 'Angular' | reverse }}</p> <!-- ralugnA -->
<p>{{ 2 | exponential:3 }}</p> <!-- 8 (2^3) -->
<p>{{ 5 | exponential:2 }}</p> <!-- 25 (5^2) -->
// Don't forget to declare pipes in module
@NgModule({
declarations: [ReversePipe, ExponentialPipe]
})Expected: Explain the core concept and Angular data flow of Custom Pipes
Show hint
Create a file-size formatting pipe and unit-test bytes, KB, MB, zero, negative, and null values.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- input and state → component and template → rendered Angular UI
- Use a custom pipe for deterministic presentation-only transformation, not business filtering or side effects. A pure pipe should produce the same result for the same arguments; expensive computation may belong in the data model or computed state. Define its name, input and output types, null behavior, and locale assumptions as a public API.
- Filtering lists with an impure pipe can rerun on every change-detection pass and harm performance.
- Create a file-size formatting pipe and unit-test bytes, KB, MB, zero, negative, and null values.
Angular — Templates — Angular