5

I have following filter in angularJS:

<div ng-repeat="node in data | filter:{parentID:12}"></div>

This works fine (I get only data, where parentID is 12).

In the next step, I want to get all data, where parentID is (for example) 12,13,25 or 30.

I tried following (its not working):

filter:{parentID:[12,13,25,30]}

Is there any way to build a filter as described?

Thank you very much!

1

1 Answer 1

6

A predicate function will suit your needs nicely! From the doc:

function(value, index): A predicate function can be used to write arbitrary
filters. The function is called for each element of array. The final result
is an array of those elements that the predicate returned true for.

For example:

<div ng-repeat="node in data | filter:checkParentID"></div>

And in your controller

$scope.checkParentID = function(value, index) {
  return value.parentID && [12,13,25,30].indexOf(value.parentID) !== -1;
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Tream Oops, I confused everything. Filter should look like filter:parentFilter. Please accept morloch's answer, because it's correct and mine is not.
Not sure, what you did wrong - works for me. But okay, I accepted his answer. Thanks you and morloch for helping me! :)
How can I achieve this but on a particular attribute of the collection
You mean an attribute of data? Just pass that to ng-repeat, e.g. node in data.attr...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.