使用otpgroup基于两个不同的数据创建选择

时间:2016-12-21 09:12:46

标签: angularjs ionic-framework angularjs-ng-repeat angularjs-ng-options optgroup

嗨朋友们在这里有两个数据在一个JSON文件中我需要根据国家/地区字段创建选择选项并根据位置字段显示数据

"countries": [
        {
            "id": "75",
            "country": "France"
        },
        {
            "id": "82",
            "country": "Germany"
        },
        {
            "id": "208",
            "country": "Switzerland"
        },
        {
            "id": "226",
            "country": "United Kingdom"
        },
        {
            "id": "227",
            "country": "United States"
        }
    ],
    location": [
        {
            "id": "49",
            "name": "Aberdeen",
            "country_id": "226",
            "country": "United Kingdom"
        },
        {
            "id": "52",
            "name": "All Cities",
            "country_id": "226",
            "country": "United Kingdom"
        },
        {
            "id": "51",
            "name": "All Cities",
            "country_id": "82",
            "country": "Germany"
        },
        {
            "id": "50",
            "name": "All Cities",
            "country_id": "227",
            "country": "United States"
        }
        {
            "id": "31",
            "name": "San Jose",
            "country_id": "227",
            "country": "United States"
        },
        {
            "id": "42",
            "name": "Seattle",
            "country_id": "227",
            "country": "United States"
        },
        {
            "id": "18",
            "name": "Stuttgart",
            "country_id": "82",
            "country": "Germany"
        },
        {
            "id": "41",
            "name": "Washington DC",
            "country_id": "227",
            "country": "United States"
        }
    ],

HTML文件

 <select ng-model="location">
                <optgroup ng-repeat="data in location"  label="{{data.country}}">
                    <option ng-repeat="item in data" value="{{data.country}}::{{item.name}}">{{item.name}}</option>
                </optgroup>
  </select>

我需要显示我的数据:

enter image description here

我尝试上面的代码,但我没有成功。请帮忙

1 个答案:

答案 0 :(得分:2)

无需使用其他阵列,您也可以通过仅使用位置数组来执行此操作

 <select ng-model="location" ng-options="option.name as option.name group by option.country for option in locations"></select>

Check this working example