Ng-selected Not Working In Select Element
Solution 2:
After playing around with ng-selected
for a while I was not able to get it to work like you're asking. However, I was able to pre-select a specific option using ng-init
.
Here's a JSFiddle of my solution. My <select>
ended up being:
<select ng-model="selectedColor" ng-options="color.value as color.name for color in colors" ng-init="selectedColor='yellow'">
<option value="">Select A Color</option>
</select>`
And my colors
array is:
colors = [
{name:'Red', value: 'red'},
{name:'Orange', value: 'orange'},
{name:'Yellow', value: 'yellow'},
{name:'Green', value: 'green'},
{name:'Blue', value: 'blue'},
{name:'Indigo', value: 'indigo'},
{name:'Violet', value: 'violet'}
]
Changing the ng-init="selectedColor='yellow'"
to another value will select a different option.
Solution 3:
Some people have problems with this. I found a great solution for a simple drop down if controller as someController
var vm = this;
this.colors = [
{name:'Red'},
{name:'Orange'},
{name:'Yellow'},
{name:'Green'},
{name:'Blue'},
{name:'Indigo'},
{name:'Violet'}
];
this.color_selected = "Yellow";
<select ng-model="someController.color_selected" ng-options="opt.name as opt.name for opt in someController.colors">
</select>
`
Solution 4:
I had a similar issue and realized the cause was because of the different data types. ng-model
was comparing against a string value but I was pulling an integer from the database so it wasn't automatically selecting the option. To overcome this, i called toString()
on the integer after querying the data from the database to ensure the data types matched.
Post a Comment for "Ng-selected Not Working In Select Element"