我想循环ng-options 50次,我该怎么办

时间:2018-05-14 14:57:45

标签: angularjs angularjs-ng-repeat ng-options angularjs-ng-options

<select  ng-model="test" ng-init="test = '1'">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    ............................
    ............................
    ............................
    <option value="48">48</option>
    <option value="49">49</option>
    <option value="50">50</option>
</select>

我尝试过这样的格式:

<select 
ng-model="test" 
ng-options="1 as One"
ng-init="test = '1'"
range="50"
>
</select>

我怎样才能像 angularjs ng-options中的循环一样, 我想循环ng-options 50次我怎么做呢

3 个答案:

答案 0 :(得分:1)

ng-repeatng-options不是foreach循环。它们采用数组,而不是输入范围。因此,在控制器中初始化一个数组并将其作为参数传递给它:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    // use your favourite filling method
    $scope.array = Array(50).fill().map((e,i)=>i+1);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">

<select ng-init="test=1" ng-model="test" ng-options="x for x in array">
</select>
{{test}}

</div>

答案 1 :(得分:1)

您可以使用ng-repeat并传入数组1到50。

<option ng-repeat="i in myArray"></option>

答案 2 :(得分:1)

您可以执行以下操作:

   <select  ng-model="test" ng-init="test = '1'">
        <option ng-repeat="number in getLoop()" ng-value="{{$index+1}}">{{$index+1}}</option>
    </select>

在您的Controller中添加以下内容:

$scope.loopSize= 5;
$scope.getLoop= function() {
    return new Array($scope.loopSize);   
}