select won't show default value when list is read from server angularjs

只谈情不闲聊 提交于 2020-01-06 04:13:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!