Iteration ng-repeat only X times in AngularJs

前端 未结 7 1887
悲哀的现实
悲哀的现实 2020-11-28 04:08

How can I use ng-repeat like for in Javascript?

example:

Text

I want to iterate wit

相关标签:
7条回答
  • 2020-11-28 04:53

    All answers here seem to assume that items is an array. However, in AngularJS, it might as well be an object. In that case, neither filtering with limitTo nor array.slice will work. As one possible solution, you can convert your object to an array, if you don't mind losing the object keys. Here is an example of a filter to do just that:

    myFilter.filter('obj2arr', function() {
        return function(obj) {
            if (typeof obj === 'object') {
                var arr = [], i = 0, key;
                for( key in obj ) {
                    arr[i] = obj[key];
                    i++;
                }
                return arr;
            }
            else {
                return obj;
            }
    
        };
    });
    

    Once it is an array, use slice or limitTo, as stated in other answers.

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