Adding ng-model to predefined Select element adds undefined option
Adding ng-model:"foo" to a prepopulated select element results in an additional "undefined" option being added and removes the effect of select:"select".
The HTML:
<select ng-model="foo" ng-app >
<option value="1">1</option>
<option selected="selected" value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
Becomes:
<select ng-model="foo" ng-app >
<option value="? undefined:undefined ?"></option>
<option value="1">1</option>
<option selected="selected" value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
In the example above the first option value="? undefined:undefined ?" is selected instead of the expected option value="2".
Reproduced at http://jsfiddle.net/K8rSu/
@trisweb :+1:
the same problem
sometimes I get this
other times I get this
Agree with @itsmechlark comment
I just came into this problem as well. @itsmechlark has a good point. Framework should not provide a selected option if none is provided within Angular. "? undefined:undefined ?" just seems irresponsible from the framework.
This worked for me.
<select ng-model="foo" ng-init="foo='1' ">
<option ng-selected="true" value="1">1</option>
<option value="1">1</option>
<option value="3">3</option>
</select>
Agree with @eyeamaman
I run across the same problem too, the option value is generated by php script
<?php while ($row = $options->fetch_array()) { ?>
<option><?= $row['title']?></option>
<?php }?>
I don't think I can use ng-init="foo='1' to set default value ,cause I don't know the value in advance.
@robinclark007, it's a workaround (for a not-really-supported usecase), but you could try setting ngInit on the selected option. (You should know the value by then, right ?).
This can be a huge pain with traditional CMS work where you want to rely on the server for a lot of data (and not create a webservis for every bit of it) but still use angular. Here is a small directive to use in the place of ng-model, which will set your model's initial value based on the static markup (value, selected, etc) https://github.com/vespoli/init-model
I wrote a workaround for this issue in the form of a directive: https://gist.github.com/Tobiaqs/97fba556349281f2db90
Include ts-select-fix in your app dependencies.
<select ng-model="data.kindOfCheese">...</select>
becomes
<select ts-select-fix ng-model="data.kindOfCheese">...</select>
This directive allows you to:
- Set a default value for a select
- Render options using ng-repeat
- Propagate UI changes to model
Plunker demonstrating the effect: https://plnkr.co/edit/TCaqT1Kr37b1jQyyRSyv?p=preview
EDIT: I just found out that setting the properties in the model as string instead of a number (partially) fixes the problem
Using 'ng-selected="true' on the option tag I wanted to show as selected worked for me (referring to gsklee's comment from 3rd September 2013)
I found the answer!!!
html:
<select id="buggySelect" ng-model="foo" ng-app > <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select>
css: #buggySelect option[value="? undefined:undefined ?"] { display:none; }
js controller: $scope.foo = "2";
this will remove the blank option and point it back to the correct place.
@Fierfek Brilliant. Thanks mate.
This happens even when the initial value is defined and is a valid option.
HTML
<select ng-model='itemIndex' ng-init='itemIndex = 0'>
<option value='{{$index}}' ng-repeat='item in lists.items' ng-selected='$parent.itemIndex === $index'>{{$index + 1}}</option>
</select>
I've tested this with and without the ngInit.
Controller
$scope.itemIndex = 0; // Initialized here even though using ng-init in the HTML.
Oddly enough, this same code works later on the page for another <select> element. The only difference is the index variable name and the list of items I repeat over (e.g. `ng-repeat='item in itemList``). Everything is initialized the same way.
@c1moore, if you think this is a different problem, please open a new issue and provide a runnable demo (e.g. using CodePen, Plnkr etc).