Custom pipes in Angular let you define your own transformation logic for displaying data in templates. They are useful when the built-in pipes (like date, uppercase, currency) do not meet your specific formatting needs.
Angular Custom Pipes PipeTransform
@Pipe decorator: Tells Angular that the class is a pipe and defines its name.PipeTransform interface: Requires a transform() method that holds transformation logic.declarations array of an Angular module.| (pipe) operator.@Pipe({ name: 'pipeName' }) above the class.PipeTransform and define transform(value: any, ...args: any[]): any.transform().ReversePipe
// reverse.pipe.ts - custom pipe that reverses strings
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse'
})
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
<!-- app.component.html - template using the reverse pipe -->
<p>Original: {{ message }}</p>
<p>Reversed: {{ message | reverse }}</p>
// app.module.ts - registering the custom pipe
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ReversePipe } from './reverse.pipe';
@NgModule({
declarations: [AppComponent, ReversePipe],
imports: [BrowserModule],
bootstrap: [AppComponent]
})
export class AppModule {}
message = 'Angular';.ReversePipe takes the string, splits it into characters, reverses them, and joins them back.AppModule, it can be used anywhere in that module’s templates.In short: custom pipes make your templates cleaner by moving display-only logic out of components and into reusable transformers.
{{ value | myPipe:arg1:arg2 }}, for flexible transformations.TitleCasePipe that capitalizes the first letter of each word in a string.NumberFormatPipe that formats a number with thousand separators (e.g. 1234567 → 1,234,567).... at the end after a given length.