How can I set the initial selection of a select field using ng-options
?
Consider the items we have:
$scope.items = [{
id: 1,
label: 'aLabel',
subItem: { name: 'aSubItem' }
}, {
id: 2,
label: 'bLabel',
subItem: { name: 'bSubItem' }
}];
The HTML code would be:
<select ng-options="item as item.label for item in items track by item.id" ng-model="selected"></select>
Instead of selecting the option with:
$scope.selected = $scope.items[0];
If I only have the ID information, I want to initialize the select with something like this:
$scope.selected = $scope.otherRandomObject.id
I believe that using track by item.id
establishes the connection between the object and its ID.