javascriptangularjsangular-ngmodel

ng-model in select box is not working correctly


I am using angular js to draw select boxes.

 <select ng-model="selected">
     <option value="{{obj.id}}" ng-repeat="obj in values">{{obj.name}} </option>
 </select>
 selected id - {{selected}} 

Here the default selected is not initialized according to value selected.

Have made a fiddle for the problem.


Solution

  • You should use ngOptions directive to render selectbox options:

    <select ng-model="selected" ng-options="obj.id as obj.name for obj in values"></select>
    

    Fixed demo: http://jsfiddle.net/qWzTb/1984/