ng-model not fillulate select

时间:2016-04-19 09:58:37

标签: angularjs drop-down-menu

这是我的目标,

$scope.addresses = [
        {
            "addressLine1": "150 Address",
            "addressLine2": "LONDON",
            "addressLine3": "",
            "addressLine4": "EC00 00X"
        },
        {
            "addressLine1": "152 Address",
            "addressLine2": "LONDON",
            "addressLine3": "",
            "addressLine4": "EC00 00X"
        },
        {
            "addressLine1": "154 Address",
            "addressLine2": "LONDON",
            "addressLine3": "",
            "addressLine4": "EC00 00X"
        }
    ];

这是我的选择HTML

<select  ng-model="model.addressChoice" ng-options="(a.addressLine1+ ', '+ a.addressLine2+ ', '+ a.addressLine3+ ', '+ a.addressLine4) for a in addresses">
            <option value="" disabled selected>{{select.length}} Address's Found</option>
        </select>

我发现使用model.addressChoice = addresses[2]时会填充选择字段,但我应该使用

mode.addressChoice = {
                "addressLine1": "154 Address",
                "addressLine2": "LONDON",
                "addressLine3": "",
                "addressLine4": "EC00 00X"
            }

这失败了,为什么?我也在这里设置了一个傻瓜作为例子。

https://plnkr.co/edit/BtxMDP0mtJx4m7PTLD6B?p=preview

2 个答案:

答案 0 :(得分:3)

它失败了,因为您正在使用此代码创建另一个实例:

        mode.addressChoice = {
            "addressLine1": "154 Address",
            "addressLine2": "LONDON",
            "addressLine3": "",
            "addressLine4": "EC00 00X"
        }

和&#34;地址&#34;在ng-options属性中设置的数组不包含此元素。您应该只使用来自&#34;地址&#34;的对象的链接。集合。

答案 1 :(得分:0)

删除disabled selected代码

中的option
    <select  
            ng-model="model.addressChoice" 
            ng-options="(a.addressLine1+ ', '+ a.addressLine2+ ', '+ a.addressLine3+ ', '+ a.addressLine4) for a in addresses">

            <option value="">{{select.length}} Address's Found</option>
 </select>

加载选中的&#34; 3发现地址&#34;