简体   繁体   English

Angular - *ngFor 在嵌套的 JSON

[英]Angular - *ngFor on nested JSON

I am trying to create a mat-option with a nested mat-option structure base on a JSON object.我正在尝试创建一个基于 JSON object 的嵌套 mat-option 结构的 mat-option。 I've tried to create a *ngFor together with a pipe to run through the structure of the JSON.我尝试创建一个 *ngFor 和一个 pipe 来运行 JSON 的结构。 So when selecting option "Actor" I'd like it to create another options fields for the elements in Actor and so on.因此,选择选项“ Actor”时,我希望它为演员中的元素创建其他选项字段。 However it only creates the "Actor"-option and then the error: "Cannot read property 'elements' of undefined" appears.但是,它只创建“演员”选项,然后出现错误:“无法读取未定义的属性'元素'”。 So somehow running through the JSON-Structure during *ngFor doesn't work the way I want it to.因此,在 *ngFor 期间以某种方式运行 JSON 结构并不能按照我想要的方式工作。 What am I missing?我错过了什么?

My HTML我的 HTML

<div *ngFor="let choice of choices; let i = index">
  <mat-form-field [ngStyle]="{'margin-left.px': 15*i}">
                  [value] = "selections?selections[i]:undefined">
  <mat-option *ngFor="let child of choice.elements | keys"
   {{child.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>

My ts + JSON + pipe:我的 ts + JSON + pipe:

  selections: string[] = [];
  this.incidentTree = 
[{"_id": {"$oid": "5e9aa4041c9d44000054070f"}, 
"Source": 
    [{"name": "Actor", 
      "id": 0, 
      "elements": [
                  {"name": "Individual",
                   "id": 0, 
                   "elements": [
                                {"name": "Outsider", 
                                 "id": 0},
                                {"name": "Insider",
                                 "id": 1}, 
                                {"name": "Trusted Insider", 
                                 "id": 2},
                                {"name": "Pirvileged Insider", "id": 3}]}, 
                 {"name": "Group", 
                  "id": 1, 
                  "elements": [
                                {"name": "Ad hoc", 
                                 "id": 0}, 
                                 {"name":"Established",
                                  "id": 1}]}, 
                 {"name": "Organzation", 
                  "id": 2,
                  "elements": [
                               {"name": "Competitor",
                                "id": 0}, 
                                {"name":"Supplier", 
                                 "id": 1},
                                {"name": "Partner",
                                 "id": 2},
                                {"name": "Customer",
                                "id": 3}]},
                               {"name": "Nation State", 
                                "id":3}]}]}]}]

  constructor() { }

  ngOnInit() {
    this.choices[0] = this.incidentTree
  }



My pipe keys:



import { Pipe, PipeTransform } from '@angular/core';
import { Source } from '../_classes/source';

@Pipe({
  name: 'keys'
})
export class KeysPipe implements PipeTransform {
  transform(value: any): any[] {
    if(value) {
      let dataArr = []
      let keyArr: any[] = Object.keys(value)

      // loop through the object,
      // pushing values to the return array
      keyArr.forEach((key: any) => {
          console.log(key)
          console.log(value[key])
          dataArr.push(value[key]);
      });

      console.log(dataArr)

      // return the resulting array
      return dataArr;
  }

}

}

What i can see from the HTML you provided is that you are missing the mat-select opening tag after the mat-form-field.从您提供的 HTML 中我可以看到,您在 mat-form-field 之后缺少 mat-select 开始标签。 Also key pipe is not required.也不需要密钥 pipe。

and most important:最重要的是:

this.choices = this.incidentTree.Source;

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

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