【问题标题】:How to display data inside object inside json/json using angular如何使用角度在 json/json 内的对象内显示数据
【发布时间】:2021-05-20 18:01:37
【问题描述】:

我得到了这个 JSON。

[
    {
        "id": 11,
        "parent_category": null,
        "name": "name11"
    },
    {
        "id": 12,
        "parent_category": {
            "id": 11,
            "name": "name11",
            "parent_category": null
        },
        "name": "name12"
    },
    {
        "id": 13,
        "parent_category": {
            "id": 11,
            "name": "name11",
            "parent_category": null
        },
        "name": "name13"
    }  
]

我想在parent_category.name 视图中显示parent_category 不为空。

  <mat-select formControlName="parent_category">
                      <mat-option>--</mat-option>
                      <mat-option *ngFor="let p_g of mydata" [value]="p_g.id">
                        {{p_g.name}}
                      </mat-option>
                    </mat-select>

拜托,你能告诉我如何展示它吗?

【问题讨论】:

    标签: json angular typescript


    【解决方案1】:

    你需要访问内部对象parent_category

    <mat-option *ngFor="let p_g of mydata" [value]="p_g.id">
          {{p_g.parent_category?.name}}
     </mat-option>
    

    【讨论】:

    • 这将呈现一个“空”选项,这可能不是 OP 想要的。
    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 2020-10-24
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多