Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select box select item from model in angular

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.

like image 452
SET Avatar asked Sep 25 '26 18:09

SET


1 Answers

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/

like image 196
jlast Avatar answered Sep 27 '26 09:09

jlast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!