Passing arguments to angularjs filters

Actually there is another (maybe better solution) where you can use the angular’s native ‘filter’ filter and still pass arguments to your custom filter. Consider the following code: <div ng-repeat=”group in groups”> <li ng-repeat=”friend in friends | filter:weDontLike(group.enemy.name)”> <span>{{friend.name}}</span> <li> </div> To make this work you just define your filter as the following: $scope.weDontLike = … Read more

How to filter array when object key value is in array

You can do it with Array.prototype.filter(), var data = { records : [{ “empid”: 1, “fname”: “X”, “lname”: “Y” }, { “empid”: 2, “fname”: “A”, “lname”: “Y” }, { “empid”: 3, “fname”: “B”, “lname”: “Y” }, { “empid”: 4, “fname”: “C”, “lname”: “Y” }, { “empid”: 5, “fname”: “C”, “lname”: “Y” }] } var empIds … Read more

AngularJS – How to structure a custom filter with ng-repeat to return items conditionally

Filters don’t work on individual items in the array, they transform the entire array into another array. userApp.filter(‘matchAccessLevel’, function() { return function( items, userAccessLevel) { var filtered = []; angular.forEach(items, function(item) { if(userAccessLevel >= item.minAccess) { filtered.push(item); } }); return filtered; }; }); See this plnkr **always inspect the arguments to a function. It’s not … Read more

How to filter (key, value) with ng-repeat in AngularJs?

Angular filters can only be applied to arrays and not objects, from angular’s API – “Selects a subset of items from array and returns it as a new array.” You have two options here: 1) move $scope.items to an array or – 2) pre-filter the ng-repeat items, like this: <div ng-repeat=”(k,v) in filterSecId(items)”> {{k}} {{v.pos}} … Read more

How to use a filter in a controller?

Inject $filter to your controller function myCtrl($scope, $filter) { } Then wherever you want to use that filter, just use it like this: $filter(‘filtername’); If you want to pass arguments to that filter, do it using separate parentheses: function myCtrl($scope, $filter) { $filter(‘filtername’)(arg1,arg2); } Where arg1 is the array you want to filter on and … Read more