SandipIntrop SandipIntrop - 1 year ago 55
AngularJS Question

dynamic options in select box using angular

I want to add option dynamically to select box using angular.
For example, if quantity limit is 4 then options should be 1, 2, 3, 4.

This is what i have tried:

<select class="form-control" ng-model="selectedQuantity">
<option value="1">1</option>
<option ng-repeat="n in range(1,edata.Products.QuantityLimit)" value="{{n}}">{{n}}</option>

Here QuantityLimit different for each select box.

Angular code:

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

this works as expected if i remove
from select. Is there any way to do this without removing

Answer Source

You can use inline ngInit directive to set an initial value to the model directly from the view:

<select class="form-control" ng-model="selectedQuantity" ng-init="selectedQuantity = 1">

And of-course, set a default value from the controller:

$scope.selectedQuantity = 1;

Choose the one you like