Passing arguments to pipes

We saw how to create pipes, but we also have it at the back of our minds that pipes take arguments. How do we add these arguments to our custom pipes?

The generated pipe could already be giving you hints from the previous example because of the optional args parameter passed to the transform method:

transform(value: any, args?: any): any {    ...}

Assuming that we want to define whether the reverse of the string is applied letter-to-letter or word-to-word, the best way to give the pipe user this control is through arguments. Here is an updated example:

export class ReversePipe implements PipeTransform {  transform(value: any, args?: any): any {    if(args){      return value.split(' ').reverse().join(' ');    } else { return value.split('').reverse().join(''); ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.