angular - 如何在 Angular 中使用嵌套 *ngFor
问题描述
我尝试在角度上嵌套,但我无法得到确切的结果。我尝试了不同的方法,但我无法得到这个嵌套循环函数的确切结果
我想要一个或多个名称明智的选择框,并且选项详细信息包含在数据字段中。
我除了嵌套或任何其他方法也可以。
请给出解决方案。
这是 .ts 文件
getSpecialisationSubSelect(id) {
this.postjobService.getSpecializationOptionChangeGetValue(id)
.subscribe(res => {
if (res !== 0){
console.log(res);
this.optionsselects = res;
this.optionsselectName = res.name;
this.optionselectSubData = res.data;
this.defaultOptions = true;
// console.log(this.getOptionsSelect(res));
}
});
}
.html 文件
<div class="col-lg-4 col-md-4" *ngFor="let opt of optionsselectName; let i = index" >
<select name="" class="form-control custom-select">
<option value="">{{ opt }}</option>
<option
class="select-option-text"
*ngFor="let optsval of optionselectSubData"
value="{{ optsval.value }}">{{ optsval.name }}
</option>
</select>
</div>
json响应
{
"count": 2,
"name": [
"Spec-1",
"Spec-1-2"
],
"list": [
16,
19
],
"data": [
[
{
"operationDropDownId": 43,
"optionMasterId": 16,
"name": "Spec-1-Option-1",
"value": "spec-1-option-1",
"status": "active",
"createdAt": "2020-07-06 10:07:33",
"updatedAt": "2020-07-06 10:07:33"
},
{
"operationDropDownId": 44,
"optionMasterId": 16,
"name": "Spec-1-Option-2",
"value": "spec-1-option-2",
"status": "active",
"createdAt": "2020-07-06 10:07:33",
"updatedAt": "2020-07-06 10:07:33"
},
{
"operationDropDownId": 45,
"optionMasterId": 16,
"name": "Spec-1-Option-3",
"value": "spec-1-option-3",
"status": "active",
"createdAt": "2020-07-06 10:07:33",
"updatedAt": "2020-07-06 10:07:33"
}
],
[
{
"operationDropDownId": 52,
"optionMasterId": 19,
"name": "Spec-1-Option-2-1",
"value": "spec-1-option-2-1",
"status": "active",
"createdAt": "2020-07-06 10:16:40",
"updatedAt": "2020-07-06 10:16:40"
},
{
"operationDropDownId": 53,
"optionMasterId": 19,
"name": "Spec-1-Option-2-2",
"value": "spec-1-option-2-2",
"status": "active",
"createdAt": "2020-07-06 10:16:40",
"updatedAt": "2020-07-06 10:16:40"
},
{
"operationDropDownId": 54,
"optionMasterId": 19,
"name": "Spec-1-Option-2-3",
"value": "spec-1-option-2-3",
"status": "active",
"createdAt": "2020-07-06 10:16:40",
"updatedAt": "2020-07-06 10:16:40"
}
]
]
}
解决方案
该data
属性是一个形式为 的数组的数组[[{}, {}, {}], [{}, {}, {}]]
。所以你需要一个*ngFor
循环来遍历元素。尝试以下
<div class="col-lg-4 col-md-4" *ngFor="let opt of optionsselectName; let i = index" >
<select name="" class="form-control custom-select">
<option value="">{{ opt }}</option>
<ng-container *ngFor="let optsval of optionselectSubData">
<option
class="select-option-text"
*ngFor="let opts of optsval"
value="{{ opts.value }}">{{ opts.name }}
</option>
</ng-container>
</select>
</div>
推荐阅读
- c++ - 如何让 C++ 从几个中选择一个随机短语?
- .net-core - 试图将一个 nuget 包推送到我们的私人订阅源。获取'加载失败:错误:02001003:系统库:fopen:没有这样的进程'
- html - 两行文本从一个字母开始(水平和垂直)
- android - GPU 驱动程序问题 - 在 Windows 10 开发环境中的 VM 内,在 vs 2019 上安装 xamarin studio
- kubernetes - docker inspect 的 kubernetes 等价物是什么?
- android - 改造 + Moshi - 找到 NoJsonAdapter
- javascript - 如何以角度在编辑表单中填充单选按钮选定的值
- java - 使用请求语言环境对休眠 bean 验证消息进行本地化
- reactjs - 有没有一种简单的方法可以用 TypeScript 开玩笑地模拟段的 AnalyticsJS analytics.track()?
- python - 如何在python-igraph中收缩节点的所有邻居