在ngOptions AngularJS中设置键值

时间:2015-11-22 17:00:53

标签: json angularjs ng-options

我正在尝试使用ngOptions创建选择下拉列表。如何从下面的数据中选择将城市名称列为选项?

所以选项是:

<option>Perugia</option>
<option>New Brunswick</option>
<option>Vicenza</option>


{
    Perugia: {
        country: [
            "IT"
         ]
    },
    New Brunswick: {
        state: [
            "NJ"
        ]
    },
    Vicenza: {
        country: [
            "IT"
        ]
    }
}

1 个答案:

答案 0 :(得分:2)

我创造了一个小提琴,检查它

https://jsbin.com/jarepo/edit?html,js,output

<select name="mySelect" id="mySelect"
  ng-options="name as name for (name, obj) in cities"
  ng-model="selected">

JS,

$scope.cities = {
  Perugia: {
    country: [
      "IT"
    ]
  },
  'New Brunswick': {
    state: [
      "NJ"
    ]
  },
  Vicenza: {
    country: [
      "IT"
    ]
  }
};
$scope.selected = "Perugia";