AngularJS limitTo filter for ngRepeat for an object (used as a dictionary)

Is it possible to use the limitTo filter in the limitTo directive, which repeats the properties of the object, and not the elements in the array.

I know that the official documentation says that the input to limitTo should be an array or a string. But I wonder if there is a way to make this work.

Here is a sample code:

 <li ng-repeat="(key, item) in phones_dict |orderBy:'-age'| limitTo:limit_items"></li> 

And $scope.phones_dict is an object like

 { item_1: {name:"John", age: 24}, item_2: {name:"Jack", age: 23} } 
+7
javascript angularjs angularjs-filter angularjs-ng-repeat
source share
4 answers

limitTo works only for strings and arrays, for example, for the object’s own use:

 myApp.filter('myLimitTo', [function(){ return function(obj, limit){ var keys = Object.keys(obj); if(keys.length < 1){ return []; } var ret = new Object, count = 0; angular.forEach(keys, function(key, arrayIndex){ if(count >= limit){ return false; } ret[key] = obj[key]; count++; }); return ret; }; }]); 

Sample script: http://jsfiddle.net/wk0k34na/2/

+14
source share

Adding

  ng-if ="$index < limit" 

The ng-repeat tag helped, although this is probably not the strictly “correct” way to do this.

+9
source share

Or you can use "track by $ index" ... And your limitTo will be:

 limitTo: $index == 5 (or any value) 
+1
source share

By default, limitTo filter supports massive objects ( example ) after 1.5.7

0
source share

All Articles