【问题标题】:Unable to set selected value in angularjs dropdown无法在 angularjs 下拉列表中设置选定值
【发布时间】:2016-05-26 15:48:16
【问题描述】:

我正在使用 html 下拉菜单并绑定来自 angularjs 函数的值,如下所示的值

function getStates() {
return [
    {
        "name": "Alabama",
        "abbreviation": "AL"
    },
    {
        "name": "Alaska",
        "abbreviation": "AK"
    }];
}

我的下拉菜单是

<select class="form-control" 
        ng-model="user.state" name="state"
        ng-options="item.name for item in  states track by item.abbreviation">
   <option value="">-- choose State --</option>
</select>

在我的范围变量 user.state 中,我得到了一个由某个函数预加载的缩写值,基于该缩写值,下拉列表的选定值应显示在 html 页面中。

欢迎有价值的cmets...

【问题讨论】:

    标签: angularjs html


    【解决方案1】:

    您需要正确的ngOptions 语法value as text for item in collection

    ng-options="item.abbreviation as item.name for item in states"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      相关资源
      最近更新 更多