I have a control. Its parameters are generated dynamically from an array of objects. In the init application, I want to select a specific option by changing the associated variable in the scope.
This does not work when the ng-option returns a full object. However, it works when select ng-option returns a string.
Is this an angular error or am I something wrong?
HTML:
<div ng-controller="selectCtrl" ng-app> Doesn't work when select ngModel value is object:<br /> <select ng-model="valueObject" ng-options="o.label for o in options"></select><br /> <pre>{{valueObject | json}}</pre> Works when select ngModel value is string:<br /> <select ng-model="valueString" ng-options="o.value as o.label for o in options"></select> <pre>{{valueString | json}}</pre>
JS:
function selectCtrl($scope) { $scope.options = [ {label: 'a', value: '1', someId: 333}, {label: 'b', value: '2', someId: 555} ]; $scope.valueObject = {label: 'a', value: '1', someId: 333}; $scope.valueString = '1'; };
JS Fiddle: http://jsfiddle.net/apuchkov/FvsKW/6/
javascript angularjs
Alexander Puchkov
source share