简体   繁体   中英

Angular what is the best way to traverse the nested object

I am trying to traverse over nested object shown below named purchase. It has sub-array called purchaseProducts which contains sub-array called products along with some other data.

What is the best way to traverse over such type of nested object?

I have tried *ngFor like this

<div *ngFor = "let data of purchase "> 
        {{data.date | slice: 0:10}}
        {{data.totalprice}}

    <div *ngFor="let product of purchase.purchaseProducts">

          {{product.id}} // not working
          {{product.quantity}} //not working
          {{product.price}}
          {{product.products.name}}
    </div>
</div>

But the inner div not displaying values.

Thank you in adavnce

在此处输入图像描述

There's no purchase_product in your json, I think you should use data.purchaseProducts :

<div *ngFor = "let data of purchase "> 
        {{data.date | slice: 0:10}}
        {{data.totalprice}}

    <div *ngFor="let product of data.purchaseProducts">

          {{product.id}} // not working
          {{product.quantity}} //not working
          {{product.price}}
          {{product.products.name}}
    </div>
</div>

Use <div *ngFor="let product of data.purchaseProducts"> instead of <div *ngFor="let product of purchase.purchase"> . This is because purchaseProduct is a key of data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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