How do I call an Angular 2 pipe with multiple arguments?
In your component's template you can use multiple arguments by separating them with colons:
{{ myData | myPipe: 'arg1':'arg2':'arg3'... }}
From your code it will look like this:
new MyPipe().transform(myData, arg1, arg2, arg3)
And in your transform function inside your pipe you can use the arguments like this:
export class MyPipe implements PipeTransform {
// specify every argument individually
transform(value: any, arg1: any, arg2: any, arg3: any): any { }
// or use a rest parameter
transform(value: any, ...args: any[]): any { }
}
Beta 16 and before (2016-04-26)
Pipes take an array that contains all arguments, so you need to call them like this:
new MyPipe().transform(myData, [arg1, arg2, arg3...])
And your transform function will look like this:
export class MyPipe implements PipeTransform {
transform(value:any, args:any[]):any {
var arg1 = args[0];
var arg2 = args[1];
...
}
}
You're missing the actual pipe.
{{ myData | date:'fullDate' }}
Multiple parameters can be separated by a colon (:).
{{ myData | myPipe:'arg1':'arg2':'arg3' }}
Also you can chain pipes, like so:
{{ myData | date:'fullDate' | myPipe:'arg1':'arg2':'arg3' }}
Since beta.16 the parameters are not passed as array to the transform()
method anymore but instead as individual parameters:
{{ myData | date:'fullDate':'arg1':'arg2' }}
export class DatePipe implements PipeTransform {
transform(value:any, arg1:any, arg2:any):any {
...
}
https://github.com/angular/angular/blob/master/CHANGELOG.md#200-beta16-2016-04-26
pipes now take a variable number of arguments, and not an array that contains all arguments.