Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 475
hard coder Avatar asked Sep 15 '25 02:09

hard coder


2 Answers

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/

like image 162
dfsq Avatar answered Sep 16 '25 16:09

dfsq


case 2 is updated in this plunker

http://jsfiddle.net/26yjn8ru/

 <div ng-repeat="arr in itr">
    <select ng-model="arr.id"
      ng-options="value.id  as value.name for value in values">
    </select>
like image 39
Divya MV Avatar answered Sep 16 '25 15:09

Divya MV