Chaining Multiple Pipes: ( (Today - Date - Uppercase - Slice:0:4) )
Chaining Multiple Pipes: ( (Today - Date - Uppercase - Slice:0:4) )
The workflow or chains will be triggered and apply the pipes one after
another. An example of the chain pipe syntax is given as follows:
{{<EXPRESSION>|<PIPE_NAME>|<PIPE_NAME>|
<PIPE_NAME>}}
@Component({
template: `
<h5>Chain Pipes</h5>
<p>Month is {{today | date | uppercase | slice:0:4}}
`,
})
export class ChainPipeComponent {
today = new Date();
}
Custom Pipes
Pipes offer a powerful mechanism to transform data, leading to a better UI
experience. In Angular 9, we can even define custom pipes to provide custom
transformation.
We will implement a custom pipe to filter data from an array bound to the view.
The @Pipe metadata decorator is used to create a custom pipe and the name of
the custom pipe is passed to this decorator. The pipe class implements the
PipeTransform interface, which provides the transform method. This method
must be implemented to write logic for the custom pipe.
The transform method accepts the value to be processed as an input parameter.
The value of the parameter is used for implementing logic of the custom pipe.
The transform method can be implemented without implementing the
PipeTransform interface in custom pipe. But implementing this interface gives
tooling support for the signature of the transform method, which can be quite
useful in large projects.
Let us take an example to understand the functioning of custom pipes.
Here's a custom pipe named ExponentialStrengthPipe that can boost a hero's
powers:
@Component({
selector: 'app-power-booster',
template: `
<h2>Power Booster</h2>
<p>Super power boost: {{2 | exponentialStrength: 10}}</p>
`
})
export class PowerBoosterComponent { }
You use your custom pipe the same way you use built-in pipes.
You must include your pipe in the declarations array of the AppModule
If you choose to inject your pipe into a class, you must provide it in
the providers array of your NgModule.