angularjsangularjs-select

Not able to set scope value on to select box


I have written code like below

<select ng-model="runnames" 
ng-options="runs.runname for runs in season.runs" 
ng-change="Onchangefunc(runnames)">

This works fine. But problem is that, i want to change the selected value of the from another function. I set the value like

$scope.runnames = 'A';

Here is how JSON looks like

[
  {"runid": "1","runame": "A"},
  {"runid": "2","runame": "B"},
  {"runid": "3","runame": "C"}
          ]

But select value does not change. Anything wrong i am doing?? or any other feature to use?


Solution

  • ng-options works on Object basis.

    Its us the key which is used in track by for setting or making option selected.

    I have Modified the HTML as follows for my own purpsose.

    <select ng-model="runnames" 
    ng-options="runs as runs.runame for runs in season track by runs.runid" 
    ng-change="Onchangefunc(runnames)">{{runs}}
    </select> 
    

    AND JS

    controller('testCtrl',function($scope){
    
        $scope.season = [
      {"runid": "1","runame": "A"},
      {"runid": "2","runame": "B"},
      {"runid": "3","runame": "C"}
              ] 
    
      $scope.runnames = {"runid": "3"}
    
    })
    

    Use the track by in your ng-options and set that value in your external function.

    Here is the plunker