Question 31
What is a custom pipe?
Apart from built-in pipes, you can write your own custom pipe with the below key characteristics:
- A pipe is a class decorated with pipe metadata
@Pipedecorator, which you import from the core Angular library For example,
@Pipe({name: 'myCustomPipe'})
- The pipe class implements the PipeTransform interface's transform method that accepts an input value followed by optional parameters and returns the transformed value.
The structure of
PipeTransformwould be as below,
interface PipeTransform {
transform(value: any, ...args: any[]): any
}
- The
@Pipedecorator allows you to define the pipe name that you'll use within template expressions. It must be a valid JavaScript identifier.
template: `{{someInputValue | myCustomPipe: someOtherValue}}`