Reputation: 7896
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?
Upvotes: 0
Views: 94
Reputation: 15292
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
Upvotes: 1
Reputation: 11
Looking at the example provided near the top of this page, https://docs.angularjs.org/api/ng/directive/ngOptions, you may need to ensure your $scope.runnames variable contains a definition for 'name'. i.e.
$scope.runnames = { name: 'abc' };
Upvotes: 0