The problem is that Angular is using object reference equality to decide if an option is selected.
Off the top of my head, I can think of two possible solutions:
- Use some sort of identifier instead of the entire object as the value for the select
- Make sure you use the actual object instead of another object that happens to have the exact same properties
For #1, change you ng-options
attribute to look like this:
valueType.code as valueType.code + ' - ' + valueType.name for valueType in getValueTypes()
(notice the valueType.code
at the beginning instead of only valueType
)
For #2, you in this instance you would need to change your initialization code, but I suspect that in a "real" application you may need to change the way that you populate values:
$scope.valueTypes = [];
// ...
$scope.getValueTypes = function () {
var valueTypes = [];
for (var i = 0; i < 5; i ++) {
var obj = {
code: 'V' + i,
name: 'Value ' + i,
description: 'Description ' + i
};
valueTypes.push(obj);
if (i == 1 || i == 3) { // or whatever
$scope.valueTypes.push(obj); // now it's the same object
}
}
return valueTypes;
};