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

廉价感情. 提交于 2019-11-30 19:37:18

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/

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.

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

limitTo: $index == 5 (or any value)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!