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

后端 未结 4 1487
独厮守ぢ
独厮守ぢ 2021-01-05 00:56

Is it possible to use limitTo filter on a ngRepeat directive which is repeating the properties of an Object and not items in an Array.

I am

相关标签:
4条回答
  • 2021-01-05 01:11

    limitTo works only for strings and arrays, for object use own filter for example:

    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;
        };
    }]);
    

    Example on fiddle: http://jsfiddle.net/wk0k34na/2/

    0 讨论(0)
  • 2021-01-05 01:29

    Default limitTo filter has support for array-like objects (example) after 1.5.7

    0 讨论(0)
  • 2021-01-05 01:30

    Or you could use 'track by $index'... And your limitTo would be:

    limitTo: $index == 5 (or any value)
    
    0 讨论(0)
  • 2021-01-05 01:36

    Adding

     ng-if ="$index < limit"
    

    to the ng-repeat tag did the trick for me, though it's probably not strictly the "right" way to do this.

    0 讨论(0)
提交回复
热议问题