JDO JDO - 1 year ago 78
AngularJS Question

How correct use ng-options?

<select class="form-control" ng-model="ad.selected" ng-options="option.name for option in ad.available track by option.value"></select>

Where i mistake? Why not set selected

options: [{value:"0",name:"First"},{value:"1",name:"Second"}]

Value from server

ad.selected // 1

Answer Source

Here is an example of how it works,

<select ng-options="option.value as option.name for option in ad.available"  ng-model="ad.selected"></select>

This generates,

  <option value="option.value">option.name</option>

option.name will be the name and option.value will display the value.

ad.selected will have to be the set as the value that is available in the array. 1 or 0.

Updated the fiddle Here is a JSFiddle