问题
I have this below select which won't show the default value when statuslist is read from the server through a http call in IE9. If statuslist values are set in the controller itself then default value show up fine.
<select id="status" class="form-control" ng-init="selectedStatus = statuslist[0]"
ng-model="selectedStatus"
ng-options="status for status in statuslist">
</select>
Default value is displayed in the dropdown if I do this
$scope.statuslist = [
"New",
"Management Review"
]
but not if the statuslist is from the server. I can see the list if I click the dropdown but not the default value.
Can someone please point at the obvious I am missing here?
回答1:
ng-init
shouldn't be used there, because it will get evaluated as the html get rendered and at that time statuslist
value was not getting loaded inside $scope
, so it will set as selectedStatus
as undefined
Better you should add this line $scope.selectedStatus = $scope.statuslist[0]
right after after the statuslist
gets loaded from the server.
来源:https://stackoverflow.com/questions/31866656/select-wont-show-default-value-when-list-is-read-from-server-angularjs