有多个参数的角管

时间:2019-08-29 13:37:51

标签: angular typescript angular-pipe

我正在使用此定制管道:

import { Pipe, PipeTransform } from '@angular/core';
import { Observable } from 'rxjs';

@Pipe({
  name: 'yesno'
})
export class YesNoPipe implements PipeTransform {
    constructor() {}

    transform(observable: Observable<Array<any>>, args: Array<any>): Observable<Array<any>> {
        console.log(args);
        return observable...
    }
}

我正在使用模板:

<div>
    {{cols | yesno:'yesno':true | async | json}}
</div>

但是,我只收到第一个参数"yesno"

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您必须在transform中使用rest parameter作为参数

// notice the three dots at: ...args
transform(observable: Observable<any[]>, ...args: any[]): Observable<any[]> {
  console.log(args);
  return observable...
}

答案 1 :(得分:-1)

您可以使用“:”传递多个参数,请参见下面的示例

{{ cols | yesno:'yesno':true | async | json }}

还请检查以下管道代码:

export class YesNoPipe implements PipeTransform {    
        transform(value:any, args:any[]):any {
                var arg1 = args[0];
                var arg2 = args[1];
                ...
        }