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

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}}</div>

And on the Controller:

$scope.filterSecId = function(items) {    var result = {};    angular.forEach(items, function(value, key) {        if (!value.hasOwnProperty('secId')) {            result[key] = value;        }    });    return result;}

jsfiddle: http://jsfiddle.net/bmleite/WA2BE/


My solution would be create custom filter and use it:

app.filter('with', function() {  return function(items, field) {        var result = {};        angular.forEach(items, function(value, key) {            if (!value.hasOwnProperty(field)) {                result[key] = value;            }        });        return result;    };});

And in html:

 <div ng-repeat="(k,v) in items | with:'secId'">        {{k}} {{v.pos}} </div>


Also you can use ng-repeat with ng-if:

<div ng-repeat="(key, value) in order" ng-if="value > 0">