javascript - 我需要在与 ngFor angular 5 相同的选择框中给出州和地区的详细信息
问题描述
这是我的 json 值,
{
"location": [{
"state": "state1",
"cities": [{
"city1": "city1"
},
{
"city2": "city2"
}
]
},
{
"state": "state2",
"cities": [{
"city1": "city1"
},
{
"city2": "city2"
},
{
"city3": "city"
}
]
}
]
}
选择框中的示例输出:
状态1 - 州名 - 父级
city1 - 城市名称 - 孩子
城市2
状态2 - 状态名称 - 父级
city1 - 城市名称 - 孩子
城市2
城市3
<select>
<option *ngFor="let locations of location">
{{locations.state}}{{locations.cities.citiname}}
</option>
<select>
它不是多选。这就像在城市下按州分组。
提前致谢。
解决方案
您可以使用元素optgroup
的属性。select
<select>
<optgroup *ngFor="let location of locations" [label]="location.state">
<option *ngFor="let city of location.cities">{{Utils.keys(city)[0]}}</option>
</optgroup>
</select>
打字稿
Utils = {
keys : Object.keys
}
您不能直接使用,Object.keys
因为Object
它是该表达式的一部分window/global
并且angular
无法评估该表达式。
推荐阅读
- css - 使用 css 调整 WordPress 中数据网格的列
- html - 框对齐很重要 CSS
- python - Python 用 tkinter 制作截屏程序
- java - 将日期传递给控制器时出现异常 - Spring MVC
- android - 在颤动中制作 CheckBoxListTile 的 DropDownButton
- javascript - 位置:固定在移动设备上无法正常工作
- kubernetes - Minikube NGINX Ingress 返回 404 Not Found
- javascript - 提交后单击 iframe 内的元素
- javascript - W2UI 表格网格在 100 条记录后未加载
- c++ - 如何更改顶点着色器中的 gl_PointSize?