I'm using AngularJS to populate a select option list.
And I'm selecting an option by default depending on a field isMandatory. But the problem is I can't get the id of the selected item on first time load.
It works when I change the option.
Please check this plunker code: http://plnkr.co/edit/U2dVeo1vwZZPFKVhPleV?p=preview
I suggest using ng-options:
<select ng-model="selectedItem" ng-options="item.id as item.text for item in list">
and a function to set the initially selected item:
angular.forEach($scope.list, function(item, key){
if(item.isMandatory) {
$scope.selectedItem = item.id;
}
});