Given this code in controller:
$scope.selectedParam = 95
$scope.param = {
value: {
95: "John",
193: "Doe",
}
}
And such a template
<select
ng-model="selectedParam"
ng-options="key as value for (key, value) in param.value track by key">
</select>
How do I make item from model to be selected?
Here is the example on jsfiddle that is not working and here is one that work.
Why first example does not work. Are there any ways to make it work? I need digits to be keys of the options object, not the values. Otherwise I would need to refactor tonnes of code in my application.
Avoid using integers as JSON-keys, JSON mandates that all keys must be strings. Then if you remove the track by you can see it working again. Html:
<select
ng-model="selectedParam"
ng-options="key as value for (key, value) in param.value"></select>
Javascript:
$scope.selectedParam = "95"
$scope.param = {
value: {
"95": "John",
"193": "Doe",
}
}
http://jsfiddle.net/bfryxsxo/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With