Kelyane Kelyane - 4 months ago 10
AngularJS Question

Set a default value in a select with simple array in AngularJS

I have a simple array:

items = ['All','item1','item2','item3'];


I have been tried

<select ng-model="ctrl.itemSelected" ng-change="ctrl.searchItem()">
<option ng-repeat="item in ctrl.items" value="{{ctrl.items.indexOf(item)}}">{{item}}</option>
</select>


However, my select is being rendered in the page as

<select ... >
<option value='?' selected> </option>
<option value='0'> All </option>
<option value='1'> Item1 </option>
<option value='2'> Item2 </option>
<option value='3'> Item3 </option>
</select>


So I would like this were
<option value='0'> All </option>
my option selected at the beginning

What can I do?

Answer

Just do in your controller to initialize itemSelected model:

ctrl.itemSelected = items[0];
Comments