Here is what seems to be bothering a lot of people (including me).
When using the ng-options
directive in AngularJS to fill in the options for a &
Selecting an item in ng-options can be a bit tricky depending on how you set the data source.
After struggling with them for a while I ended up making a sample with most common data sources I use. You can find it here:
http://plnkr.co/edit/fGq2PM?p=preview
Now to make ng-options work, here are some things to consider:
key | label
. Many online examples put objects as 'key', and if you need information from the object set it that way, otherwise use the specific property you need as key. (ID, CODE, etc.. As in the plckr sample)The way to set the value of the dropdown/select control depends on #3,
$scope.dropdownmodel = $scope.user.state;
If you set the object as key, you need to loop trough the options, even assigning the object will not set the item as selected as they will have different hashkeys, e.g.:
for (var i = 0, len = $scope.options.length; i < len; i++) {
if ($scope.options[i].id == savedValue) { // Your own property here:
console.log('Found target! ');
$scope.value = $scope.options[i];
break;
}
}
You can replace savedValue for the same property in the other object, $scope.myObject.myProperty
.