AngularJS - value attribute for select AngularJS - value attribute for select angularjs angularjs

AngularJS - value attribute for select


What you first tried should work, but the HTML is not what we would expect. I added an option to handle the initial "no item selected" case:

<select ng-options="region.code as region.name for region in regions" ng-model="region">   <option style="display:none" value="">select a region</option></select><br>selected: {{region}}

The above generates this HTML:

<select ng-options="..." ng-model="region" class="...">   <option style="display:none" value class>select a region</option>   <option value="0">Alabama</option>   <option value="1">Alaska</option>   <option value="2">American Samoa</option></select>

Fiddle

Even though Angular uses numeric integers for the value, the model (i.e., $scope.region) will be set to AL, AK, or AS, as desired. (The numeric value is used by Angular to lookup the correct array entry when an option is selected from the list.)

This may be confusing when first learning how Angular implements its "select" directive.


You can't really do this unless you build them yourself in an ng-repeat.

<select ng-model="foo">   <option ng-repeat="item in items" value="{{item.code}}">{{item.name}}</option></select>

BUT... it's probably not worth it. It's better to leave it function as designed and let Angular handle the inner workings. Angular uses the index this way so you can actually use an entire object as a value. So you can use a drop down binding to select a whole value rather than just a string, which is pretty awesome:

<select ng-model="foo" ng-options="item as item.name for item in items"></select>{{foo | json}}


If you use the track by option, the value attribute is correctly written, e.g.:

<div ng-init="a = [{label: 'one', value: 15}, {label: 'two', value: 20}]">    <select ng-model="foo" ng-options="x for x in a track by x.value"/></div>

produces:

<select>    <option value="" selected="selected"></option>    <option value="15">one</option>    <option value="20">two</option></select>