0

Just trying to figure out how to go about doing this. The basic idea of what I am trying to achieve is this

<div ng-repeat="product in products | filter: myFilter">

$scope.myFilter = function (item) { 
    return item === 'red' || item === 'blue'; 
};

However in my case, my filter parameters are stored in an array that will be changing dynamically based on user input. I have tried this but it won't work as I am returning in the loop.

var index;
$scope.filterParams = ['red', 'blue']

$scope.myFilter = function (item) {
    for (index = 0; index < $scope.filterParams.length; ++index) {
        return item === $scope.filterParams[index];
    }
};

Help is appreciated, thanks!

2 Answers 2

1

Try this:

app.filter('myFilter ', function() {
   $scope.filterParams = ['red', 'blue'];

   return function(item) {
      return  $scope.filterParams.indexOf(item) !== -1;
   });
});
Sign up to request clarification or add additional context in comments.

Comments

1

The issue is that you are returning on the first iteration. You could do this instead:

$scope.myFilter = function(item) {
    return $scope.filterParams.indexOf(item) > -1;
};

Comments

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.