JDO JDO - 2 years ago 109
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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download