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!
Answer: A is the correct option. The syntax of applying multiple filters in AngularJS can be written as: {{ expression | filter1 | filter2 | ... }}
In AngularJS, you can also inject the $filter service within the controller and can use it with the following syntax for the filter. Syntax: $filter("filter")(array, expression, compare, propertyKey) function myCtrl($scope, $filter) { $scope. finalResult = $filter("filter")( $scope.
The pipe symbol ( | ) is used to chain multiple filters together.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With