I'm experiencing an issue if I set ng-options from within a $scope.$watch statement.
The following works:
<select ... ng-options="x.val as x.id for x in options"></select>
But this doesn't:
<select ... ng-options="x.val as x.val for x in options"></select>
Plunker showing my issue -- click 'update options'
It seems if the model's value is set before the options are, and the label and value are equivalent, the model will not match any available option.
Is this a bug, or am I doing something wrong?
sample controller code:
$scope.myModel = 'Two';
$scope.$watch('loadTrigger', function(newValue, oldValue) {
if (newValue == oldValue) { return; }
$scope.options = [{id: 1, text: 'One'}, {id: 2, text: 'Two'}...];
})
sample view:
<select ng-model="myModel" ng-options="x.text as x.text for x in options">...</select>
When the watch triggers and the options get updated, the select will display a blank or default option, rather than the expected "Two". If the label and value differ (even slightly), however, everything works as expected