gpt4 book ai didi

javascript - 使用 AngularJS 使用对象数组填充和过滤下拉列表

转载 作者:可可西里 更新时间:2023-11-01 13:00:05 29 4
gpt4 key购买 nike

我有以下来自 API 的数据结构。

$scope.cityList = [{
"_id": {
"$oid": "584ebd7f734d1d55b6dd4e5e"
},
"cityName": "New York",
"region": "north",
"people": [
{ "id": 1, "name": "x" },
{ "id": 2, "name": "y" },
{ "id": 3, "name": "z" },
{ "id": 4, "name": "a" },
{ "id": 5, "name": "b" },
{ "id": 6, "name": "c" }
]
},
{
"_id": {
"$oid": "584ebd7f734d1d55b6dd4e5e"
},
"cityName": "New Jersey",
"region": "South",
"people": [
{ "id": 1, "name": "x" },
{ "id": 2, "name": "y" },
{ "id": 3, "name": "z" },
{ "id": 4, "name": "a" },
{ "id": 5, "name": "b" },
{ "id": 6, "name": "c" }
]
}]

我正在尝试设置两个下拉菜单:

  • 第一个显示 cityNames 的列表
  • 第二个显示来自所选城市的所有人姓名的列表。

我还想将所选用户名的id保存到一个变量中。


到目前为止我已经试过了:

<select ng-model="city">
<option ng-repeat="city in cityList" value="{{city.cityName}}">{{city.cityName}}</option>
</select>

<select ng-model="selectedItem">
<optgroup ng-repeat="option in cityList | filter: city">
<option ng-repeat="x in option.people">{{x}}</option>
</select>

最佳答案

你应该使用 ng-options :

<select ng-model="selectedCity" 
ng-options="c as c.cityName for c in cityList">
</select>

<select ng-model="selectedPerson"
ng-options="p as p.name for p in selectedCity.people">
</select>

Demo on JSFiddle.

关于javascript - 使用 AngularJS 使用对象数组填充和过滤下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41117227/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com