Angular ngFor,将多参数函数传递给管道

时间:2018-06-07 12:13:53

标签: angular function pipe parameter-passing

我正在使用'其中'管道在这个链接:

https://github.com/fknop/angular-pipes/blob/master/docs/array.md#where

可以使用这样的函数:

const values = [{
    a: 1,
    c: {
        d: 3,
        e: {
            f: 4
        }
    }
}, {
    a: 2,
    c: {
        d: 4,
        e: {
            f: 5
        }
    }
}];

const numbers = [1, 2, 3, 4, 1, 4];

// ...

aEqualsOne(item) {
    return item.a === 1;
}

//usage example :
{{ values | where: aEqualsOne }} <!-- [{ a: 1, c: { d: 3, e: { f: 4 } }] -->

现在我有了这个ngFor:

<card  *ngFor="let num of listOfNum | where: checkNum></card>

在组件中使用此函数:

checkNum(num) {
    console.log(num);
    return num > 10;
}

但我必须修改函数以接受另一个参数:

checkNum(num, k) {
    console.log(num + " > " + k + "?");
    return num > k;
}

问题是我无法找到通过列表项目的方法,而且&#39; k&#39;传递给管道的函数:

我尝试过:

<card  *ngFor="let num of listOfNum | where: checkNum(k)></card>

<card  *ngFor="let num of listOfNum | where: checkNum(num, k)></card>

但现在没有成功..

我该怎么办?

1 个答案:

答案 0 :(得分:1)

修改:

<card  *ngFor="let num of listOfNum | where: checkNum(k)></card>

到:

<card  *ngFor="let num of listOfNum | where: checkNum : k></card>