0

When I am binding my dropdown by using ng-option, the code value change from string to 0, 1, 2.... here is my code

<select ng-model="class.code"
   data-ng-form="field"
   data-ng-required="false"
   ng-options="item.code as item.name for item in classes"
   class="form-control" />

Here is the json which I used to bind the drop down

class.code : 'EQ';
classes: [ { "code": "BU ", "name": "BU " },  
  { "code": "EQ ", "name": "EQ " }, 
  { "code": "IO ", "name": "IO " }, 
  { "code": "LA ", "name": "LA " }, 
  { "code": "VH ", "name": "VH " } ] 

Copied from browser (check value)

<select ng-model="class.code" 
   data-ng-form="field" 
   data-ng-required="false" 
   ng-options="item.code as item.name for item in classes" 
   class="form-control">
   <option selected="selected" value="?"></option>
   <option value="0">BU</option>
   <option value="1">EQ</option>
   <option value="2">IO</option>
   <option value="3">LA</option>
   <option value="4">VH</option>
</select>

Why the value is not being used which I passed in my array rather than 0, 1 2 3 ...

Check this:

http://jsfiddle.net/U3pVM/9615/

Ali Adravi
  • 21,707
  • 9
  • 87
  • 85

1 Answers1

0

There are spaces at the end of your codes, but not class.code. Also, class should be an object - not a function:

$scope.class = {
  code: 'EQ '
}; 

I added a trailing space to class.code, to match classes. http://jsfiddle.net/U3pVM/9616/

...

Also, you are missing the closing </select> tag.

j.wittwer
  • 9,497
  • 3
  • 30
  • 32
  • If there would be any space then it will contvert to 0 1 2 3 ...., my problem is this – Ali Adravi Oct 08 '14 at 20:53
  • angular doesn't really care about the value attributes on each option. if you check out my updated fiddle, you can see that the correct value is getting set. http://jsfiddle.net/U3pVM/9653/ – j.wittwer Oct 09 '14 at 02:20
  • i haven't tried yet, but it looks like a tracking expression might be able to set the option value.. if it really matters. http://stackoverflow.com/questions/12139152/how-to-set-value-property-in-angularjs-ng-options – j.wittwer Oct 09 '14 at 02:20