当从服务器角度读取列表时,select将不显示默认值 [英] select won't show default value when list is read from server angularjs

查看:200
本文介绍了当从服务器角度读取列表时,select将不显示默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的选择中,当通过IE9中的http调用从服务器读取状态列表时,不会显示默认值。如果状态列表值在控制器本身中设置,则默认值显示为正常。

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?

推荐答案

ng-init 不应该在那里使用,因为它将被评估为html在 $ scope 中,$ code> statuslist 的值没有被加载,所以它将设置为 selectedStatus as undefined

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

更好的是你应该添加这一行 $ scope.selectedStatus = $ scope.statuslist [0] 紧接着 statuslist 从服务器加载。

Better you should add this line $scope.selectedStatus = $scope.statuslist[0] right after after the statuslist gets loaded from the server.

这篇关于当从服务器角度读取列表时,select将不显示默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆