Custom pipes help you change how data looks in your app. They make your templates cleaner and your code easier to read.
Creating custom pipes in Angular
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'pipeName' }) export class PipeNamePipe implements PipeTransform { transform(value: any, ...args: any[]): any { // your transformation logic here return transformedValue; } }
The @Pipe decorator defines the pipe's name used in templates.
The transform method takes the input value and optional arguments, then returns the transformed output.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'exclaim' }) export class ExclaimPipe implements PipeTransform { transform(value: string): string { return value + '!'; } }
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'truncate' }) export class TruncatePipe implements PipeTransform { transform(value: string, limit: number): string { return value.length > limit ? value.substring(0, limit) + '...' : value; } }
This example shows a custom pipe named reverse that flips a string backwards. The component uses it to show the original and reversed text.
import { Component } from '@angular/core'; import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'reverse' }) export class ReversePipe implements PipeTransform { transform(value: string): string { return value.split('').reverse().join(''); } } @Component({ selector: 'app-root', standalone: true, imports: [ReversePipe], template: `<h1>Original: {{ text }}</h1><h2>Reversed: {{ text | reverse }}</h2>` }) export class AppComponent { text = 'hello'; }
Always name your pipes with lowercase letters and no spaces for easy use in templates.
Custom pipes should be pure by default, meaning they only change output when input changes, for better performance.
Remember to add your pipe to the module or use standalone components to make it available.
Custom pipes let you change how data looks in your Angular templates.
They use a transform method to convert input to output.
Use them to keep your templates clean and reuse formatting logic.
Practice
Solution
Step 1: Understand the role of pipes in Angular
Pipes are used to transform data before displaying it in templates.Step 2: Identify what custom pipes do
Custom pipes let you create your own data transformations to keep templates clean and reusable.Final Answer:
To transform data in templates for display -> Option DQuick Check:
Custom pipes = transform data [OK]
- Confusing pipes with services
- Thinking pipes handle HTTP or routing
- Mixing pipes with component lifecycle
Solution
Step 1: Recall Angular pipe class requirements
A custom pipe class must implement the PipeTransform interface and define a transform method.Step 2: Check each option
export class MyPipe implements PipeTransform { transform(value: any): any { return value; } } correctly implements PipeTransform and has transform method. export class MyPipe { transform(value: any): any { return value; } } misses interface. export function MyPipe(value: any): any { return value; } is a function, not a class. export class MyPipe implements OnInit { transform(value: any): any { return value; } } wrongly implements OnInit instead of PipeTransform.Final Answer:
export class MyPipe implements PipeTransform { transform(value: any): any { return value; } } -> Option AQuick Check:
Pipe class implements PipeTransform [OK]
- Forgetting to implement PipeTransform
- Using a function instead of a class
- Implementing wrong interfaces like OnInit
{{ 'hello' | exclaim }} if the pipe is defined as:import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'exclaim' })
export class ExclaimPipe implements PipeTransform {
transform(value: string): string {
return value + '!';
}
}Solution
Step 1: Understand the transform method behavior
The transform method appends an exclamation mark to the input string.Step 2: Apply the pipe to the input 'hello'
Input 'hello' becomes 'hello!' after transformation.Final Answer:
hello! -> Option CQuick Check:
Input + '!' = hello! [OK]
- Ignoring the transform method logic
- Assuming pipe adds prefix instead of suffix
- Not registering the pipe in module (causing errors)
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'reverse' })
export class ReversePipe {
transform(value: string): string {
return value.split('').reverse().join('');
}
}Solution
Step 1: Check class declaration
The class ReversePipe does not implement PipeTransform interface, which is required for custom pipes.Step 2: Verify transform method presence
The transform method exists and is correctly implemented.Final Answer:
The class does not implement PipeTransform interface -> Option AQuick Check:
Pipe class must implement PipeTransform [OK]
- Omitting PipeTransform interface
- Misnaming the transform method
- Incorrect pipe decorator usage
Solution
Step 1: Handle zero case explicitly
Zero is a special case and must return 'Zero'.Step 2: Check even or odd for other numbers
Use modulo operator to check if number is even or odd and return accordingly.Final Answer:
transform(value: number): string { if (value === 0) return 'Zero'; return value % 2 === 0 ? 'Even' : 'Odd'; } -> Option BQuick Check:
Zero check first, then even/odd [OK]
- Not handling zero separately
- Returning wrong labels for zero
- Using incorrect conditions for even/odd
Ready to prove it?
Pass the test and this pattern moves unknown
