Kelyane Kelyane - 1 year ago 60
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>

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>

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

What can I do?

Answer Source

Just do in your controller to initialize itemSelected model:

ctrl.itemSelected = items[0];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download