简体   繁体   English

如何在 Angular 中使用嵌套 *ngFor

[英]How To use Nested *ngFor in Angular

i tried nested for on angular but i can't get exact result.我尝试在 angular 上嵌套,但我无法得到准确的结果。 i tried different way but i can't got the exact result of this nested loop function我尝试了不同的方法,但我无法得到这个嵌套循环 function 的确切结果

i want the name wise select box one or more and the option details are contained data field.我想要名称明智的 select 框一个或多个,选项详细信息包含数据字段。

i except the nested or any other methods are also okey.我除了嵌套或任何其他方法也可以。

please give the solution.请给出解决方案。

this is.ts file这是.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 file 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 response 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"
            }
        ]
    ]
}

The data property is an array of arrays of the form [[{}, {}, {}], [{}, {}, {}]] . data属性是[[{}, {}, {}], [{}, {}, {}]]形式的 arrays 数组。 So you need one more *ngFor loop to iterate through the elements.因此,您还需要一个*ngFor循环来遍历元素。 Try the following尝试以下

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM