如何获取相应的下拉选择对象数组作为列表?

时间:2017-04-17 14:10:26

标签: javascript angularjs

我有一个包含对象数组(国家/地区)的基本下拉列表,对于每个对象,都会有对象数组(城市)。因此,如果我在下拉列表中选择一个国家/地区,则必须在ng-change上显示该对象中的城市列表。

这是我的代码:

JavaScript的:

   $scope.data = [
   {
  "id": 1,
  "countryName": "India",
  "cities": [
    {
        "id": 11,
        "cityName": "hyderabad"
    },
    {
        "id": 12,
        "cityName": "Bengaluru"
    },
    {
        "id": 13,
        "cityName": "Mumbai"
    },
    {
        "id": 14,
        "cityName": "Delhi"
    }
   ]
 },
{
  "id": 2,
  "countryName": "Afghanisthan",
  "cities": [
    {
        "id": 21,
        "cityName": "kabul"
    },
    {
        "id": 22,
        "cityName": "kandahar",
    },
    {
        "id": 23,
        "cityName": "herat"
    }
  ]
},
{
  "id": 3,
  "countryName": "Alaska",
  "cities": [
    {
        "id": 31,
        "cityName": "Fairbanks"
    },
    {
        "id": 32,
        "cityName": "Juneau"
    },
    {
        "id": 33,
        "cityName": "Anchorage"
    }
  ]
}
]

HTML:

        <select>
            <option ng-repeat="user in data | filter:search:strict" ng-
       click="selectedItem()">{{user.countryName}}</option>

        </select>

1 个答案:

答案 0 :(得分:1)

<select ng-options="n as n.countryName for n in data" ng-model="selected">      
  </select>
<ul>
  <li ng-repeat="n in selected.cities">
    {{n.cityName}}
  </li>
</ul>