Way to ng-repeat defined number of times instead of repeating over array?

前端 未结 26 3603
刺人心
刺人心 2020-11-22 14:53

Is there a way to ng-repeat a defined number of times instead of always having to iterate over an array?

For example, below I want the list item to show

26条回答
  •  借酒劲吻你
    2020-11-22 15:25

    Angular provides filters to modify a collection. In this case the collection would be null, i.e. [], and the filter also takes arguments, as follows:

    • {{$index}}

    JS:

    module.filter('fixedNumber', function() {
        return function(emptyarray, number) {
            return Array(number);
        }
    });
    
    module.controller('myCtrl', ['$scope', function($scope) {
        $scope.number = 5;
    }]);
    

    This method is very similar to those proposed above and isn't necessarily superior but shows the power of filters in AngularJS.

提交回复
热议问题