select won't show default value when list is r

2019-08-09 11:17发布

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条回答
贪生不怕死
2楼-- · 2019-08-09 11:42

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.

查看更多
登录 后发表回答