在一个选择框Angularjs设置一个已经存在的选定属性的默认选定值 [英] Set an already existing selected attribute to the default selected value in a select box Angularjs

查看:110
本文介绍了在一个选择框Angularjs设置一个已经存在的选定属性的默认选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写有一个人对象中Angular.js应用程序和人的对象中有此人的状态的属性。看样物体:

I'm writing an Angular.js application which has a person object and within that person object there is an attribute for the person's state. See sample object:

{
    "name":"John Doe",
    "address":"555 Test Avenue",
    "city":"Some City",
    "state":{
        "code":"AZ",
        "name":"Arizona"
    }
    ...
}

然后,在我的选择,我列出了所有的状态。见下图:

Then, in my select, I am listing all the states. See below:

<select ng-options="state.name for state in states" ng-model="person.state"></select>

所以,我认为应该发生的事情是,既然人已经有了一个国家分配给他们的,我想在默认选中选项&LT;选择&GT; 来是人的状态。我会想到,因为 NG-模型设置为 person.state ,它会默认选择该状态但事实并非如此。任何人都知道如何使它因此它会吗?谢谢你。

So what I think should be happening is that, since the person already has a state assigned to them, I want the default selected option in the <select> to be the person's state. I would have thought, since the ng-model is set to person.state that it would select that state by default, but it doesn't. Anyone know how to make it so it would? Thanks.

推荐答案

由于黑洞指着我的答案方向:

Thanks to Blackhole for pointing me in the direction of the answer:

角度进行比较,以供参考,而不是平等。我所要做的就是找​​到状态数组中的正确状态的指标,并设置 person.state 对象引用的状态对象数组中为止。现在,它的伟大工程!由于黑洞的指导我在正确的方向!

Angular compares for reference rather than for equality. All I had to do was find the index of the correct state in the states array and set the person.state object to reference THAT state object in the array. Now it works great! Thanks Blackhole for steering me in the right direction!

这篇关于在一个选择框Angularjs设置一个已经存在的选定属性的默认选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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