AngularJS – Filter empty objects
We can simply use ng-if here: <div ng-repeat=”data in myData ” ng-if=”data.Message”> {{ data.ID }} {{ data.Message }} </div>
We can simply use ng-if here: <div ng-repeat=”data in myData ” ng-if=”data.Message”> {{ data.ID }} {{ data.Message }} </div>
If you want to run some custom filter logic you can create a function which takes the array element as an argument and returns true or false based on whether it should be in the search results. Then pass it to the filter instruction just like you do with the search object, for example: JS: … Read more
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
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
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
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
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