Search code examples
angularjsangularjs-ng-repeatng-optionsangularjs-ng-options

i want to loop ng-options for 50 times how can i do it


<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>

I have tried something like this i need in this format:

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

How can i make it like for loop in angularjs ng-options, i want to loop ng-options for 50 times how can i do it


Solution

  • ng-repeat or ng-options are not foreach loops. They take an array, not range for an input. So initialise an array in the controller and pass it there as a parameter:

    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>