Stack Diego
Stack Diego

Reputation: 1337

Angular ngFor, pass a multiple parameter function to pipe

I'm using the 'where' pipe at this link:

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

which can be used with a function like this:

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 } }] -->

Now I have this ngFor:

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

with this function in the component:

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

but I have to modify the function to accept another parameter:

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

The problem is that I cannot find a way to pass both the item of the list and 'k' to the function passed to the pipe:

I tried with:

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

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

but no success for now..

What should I do?

Upvotes: 0

Views: 815

Answers (1)

Bhanu Tej P
Bhanu Tej P

Reputation: 230

Modify :

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

to :

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

Upvotes: 1

Related Questions