0 day streak
javascript / beginner
Snippet

Data Transformation with Custom Pipes

Custom pipes are used to encapsulate data formatting logic that can be reused across different templates in your application.

snippet.js
javascript
1
2
3
4
5
6
@Pipe({ name: 'square' })
export class SquarePipe implements PipeTransform {
transform(value: number): number {
return value * value;
}
}
angular
Breakdown
1
@Pipe({ name: 'square' })
Defines the name that will be used to apply the pipe in HTML.
2
implements PipeTransform
Ensures the class follows the required structure for an Angular pipe.
3
transform(value: number)
The method that takes the input value and returns the formatted result.