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>

当前回答

我使用自定义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>

其他回答

方法定义

下面的代码定义了一个方法range(),可用于应用程序MyApp的整个作用域。它的行为非常类似于Python的range()方法。

angular.module('MyApp').run(['$rootScope', function($rootScope) {
    $rootScope.range = function(min, max, step) {
        // parameters validation for method overloading
        if (max == undefined) {
            max = min;
            min = 0;
        }
        step = Math.abs(step) || 1;
        if (min > max) {
            step = -step;
        }
        // building the array
        var output = [];
        for (var value=min; value<max; value+=step) {
            output.push(value);
        }
        // returning the generated array
        return output;
    };
}]);

使用

只有一个参数:

<span ng-repeat="i in range(3)">{{ i }}, </span>

0, 1, 2,

有两个参数:

<span ng-repeat="i in range(1, 5)">{{ i }}, </span>

One, two, three, four,

有三个参数:

<span ng-repeat="i in range(-2, .7, .5)">{{ i }}, </span>

-2, -1.5, -1, -0.5, 0, 0.5,

我想出了一个更简单的版本,用于创建两个定义的数字之间的范围,例如。5至15

参见JSFiddle上的演示

HTML:

<ul>
    <li ng-repeat="n in range(5,15)">Number {{n}}</li>
</ul>

控制器:

$scope.range = function(min, max, step) {
    step = step || 1;
    var input = [];
    for (var i = min; i <= max; i += step) {
        input.push(i);
    }
    return input;
};

嗨,你可以使用AngularJS实现纯html(不需要指令!)

<div ng-app="myapp" ng-controller="YourCtrl" ng-init="x=[5];">
  <div ng-if="i>0" ng-repeat="i in x">
    <!-- this content will repeat for 5 times. -->
    <table class="table table-striped">
      <tr ng-repeat="person in people">
         <td>{{ person.first + ' ' + person.last }}</td>
      </tr>
    </table>
    <p ng-init="x.push(i-1)"></p>
  </div>
</div>

这是最简单的变体: 只需使用整数数组....

< lig repeat= < n >测试:

在控制器中设置作用域

var range = [];
for(var i=20;i<=70;i++) {
  range.push(i);
}
$scope.driverAges = range;

在“Html模板文件”中设置“重复”

<select type="text" class="form-control" name="driver_age" id="driver_age">
     <option ng-repeat="age in driverAges" value="{{age}}">{{age}}</option>
</select>