Yogesh Mangaj Yogesh Mangaj - 9 months ago 228
AngularJS Question

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

Is it possible to use

filter on a
directive which is repeating the properties of an Object and not items in an Array.

I am aware that the official documentation says that the input to
needs to be an array or a string. But wondering if there's a way to get this to work.

Here's a sample code:

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

is an Object like

item_1: {name:"John", age: 24},
item_2: {name:"Jack", age: 23}


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];
        return ret;

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