Angular确实在HTML指令中提供了一些使用数字的for循环的支持:

<div data-ng-repeat="i in [1,2,3,4,5]">
  do something
</div>

但是,如果作用域变量包含一个具有动态数字的范围,那么每次都需要创建一个空数组。

在控制器中

var range = [];
for(var i=0;i<total;i++) {
  range.push(i);
}
$scope.range = range;

在HTML中

<div data-ng-repeat="i in range">
  do something
</div>

这是可行的,但这是不必要的,因为我们在循环中根本不会使用范围数组。有人知道设置最小/最大值的范围或规则吗?

喜欢的东西:

<div data-ng-repeat="i in 1 .. 100">
  do something
</div>

当前回答

这是jzm改进后的答案(我不能评论,否则我会评论她/他的答案,因为她/他包含错误)。 函数有一个开始/结束范围值,所以它更灵活,而且……它的工作原理。这个特殊的例子是在一个月的某一天:

$scope.rangeCreator = function (minVal, maxVal) {
    var arr = [];
   for (var i = minVal; i <= maxVal; i++) {
      arr.push(i);
   }
   return arr;
};


<div class="col-sm-1">
    <select ng-model="monthDays">
        <option ng-repeat="day in rangeCreator(1,31)">{{day}}</option>
    </select>
</div>

其他回答

使用UnderscoreJS:

angular.module('myModule')
    .run(['$rootScope', function($rootScope) { $rootScope.range = _.range; }]);

将此应用到$rootScope使其在任何地方都可用:

<div ng-repeat="x in range(1,10)">
    {{x}}
</div>

最简单的无代码解决方案是初始化一个数组的范围,并使用$index +无论多少我想通过偏移:

<select ng-init="(_Array = []).length = 5;">
    <option ng-repeat="i in _Array track by $index">{{$index+5}}</option>
</select>

@Mormegil的解决方案的改进

app.filter('makeRange', function() {
  return function(inp) {
    var range = [+inp[1] && +inp[0] || 0, +inp[1] || +inp[0]];
    var min = Math.min(range[0], range[1]);
    var max = Math.max(range[0], range[1]);
    var result = [];
    for (var i = min; i <= max; i++) result.push(i);
    if (range[0] > range[1]) result.reverse();
    return result;
  };
});

使用

<span ng-repeat="n in [3, -3] | makeRange" ng-bind="n"></span>

Three, two, one, zero, one, two, three

<span ng-repeat="n in [-3, 3] | makeRange" ng-bind="n"></span>

Minus 3, minus 2, minus 1, 0, 1, 2, 3

<span ng-repeat="n in [3] | makeRange" ng-bind="n"></span>

0, 1, 2, 3

<span ng-repeat="n in [-3] | makeRange" ng-bind="n"></span>

0-1-2-3

很简单:

$scope.totalPages = new Array(10);

 <div id="pagination">
    <a ng-repeat="i in totalPages track by $index">
      {{$index+1}}
    </a>   
 </div> 

我使用自定义ng-repeat-range指令:

/**
 * Ng-Repeat implementation working with number ranges.
 *
 * @author Umed Khudoiberdiev
 */
angular.module('commonsMain').directive('ngRepeatRange', ['$compile', function ($compile) {
    return {
        replace: true,
        scope: { from: '=', to: '=', step: '=' },

        link: function (scope, element, attrs) {

            // returns an array with the range of numbers
            // you can use _.range instead if you use underscore
            function range(from, to, step) {
                var array = [];
                while (from + step <= to)
                    array[array.length] = from += step;

                return array;
            }

            // prepare range options
            var from = scope.from || 0;
            var step = scope.step || 1;
            var to   = scope.to || attrs.ngRepeatRange;

            // get range of numbers, convert to the string and add ng-repeat
            var rangeString = range(from, to + 1, step).join(',');
            angular.element(element).attr('ng-repeat', 'n in [' + rangeString + ']');
            angular.element(element).removeAttr('ng-repeat-range');

            $compile(element)(scope);
        }
    };
}]);

HTML代码是

<div ng-repeat-range from="0" to="20" step="5">
    Hello 4 times!
</div>

或者简单地

<div ng-repeat-range from="5" to="10">
    Hello 5 times!
</div>

或者简单地说

<div ng-repeat-range to="3">
    Hello 3 times!
</div>

或者只是

<div ng-repeat-range="7">
    Hello 7 times!
</div>