1

In my template I use ng-repeat="obj in list".

Where list is object:

0: {
  status : "active",
  category : "animal"
}
...

Also by default I have $scope.filter = {}

When I handle click: I fill this $scope.filter:

$scope.filter["categories"].push("animal");
$scope.filter["categories"].push("human");

How to make that filter in ng-repeat will work with array $scope.filter["categories"] and will find elements where category is corresponding $scope.filter["categories"]?

1
  • The easiest way is probably to write your own custom filter. Commented Mar 7, 2017 at 17:50

2 Answers 2

3

You can have custom filter.

app.filter('myFilter', function() {

      return function(arrList, arrKeywords) {

          var arrFilterList = [];

          angular.forEach(arrList,function(oneVal,key){

              if(arrKeywords.indexOf(oneVal)){
                arrFilterList.push(oneVal);  
              }
          })

          return arrFilterList;
      }
    });
Sign up to request clarification or add additional context in comments.

Comments

1

create a custom filter like this

angular.module("app",[])
.filter('sam',function(){
  return function(item, arr){
    return arr.find(function(obj){ 
      return obj === item;
    })
  }
})

angular.module("app",[])
.filter('sam',function(){
  return function(item, arr){
    return arr.find(function(obj){ 
      return obj === item;
    })
  }
})
.controller("ctrl",function($scope){
	$scope.list = [{
  status : "active",
  category : "animal"
},{
  status : "active",
  category : "human"
},{
  status : "active",
  category : "bird"
}]

$scope.filter = {"categories":[]}

$scope.filter["categories"].push("animal");
$scope.filter["categories"].push("human");
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div ng-repeat="obj in list ">
{{obj.category | sam : filter.categories}}
</div>

</div>

3 Comments

Why not a filter at the collection level (so it can be applied at ng-repeat)?
because then the filter pass the whole list array instead of an object and need a another additional loop to filter the relevant value
Angular will filter before the ng-repeat loop so I'm not sure that the idea of "another additional loop" is correct here. I would think best practice would be to filter at the collection level.

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.