我无法遍历对象Object

时间:2020-11-01 21:38:59

标签: angular typescript ngfor

我像这样嵌套了JSON

public class GlazingSimpleMaterial 
{
    public Guid Id { get; set; }
    .....
    .....
}
public class GlazingComplexMaterial 
{
    public Guid Id { get; set; }
    .....
    .....
}
public class GlazingGasMaterial 
{
    public Guid Id { get; set; }
    .....
    .....
}

enter image description here

我需要遍历所有子类别并获得子类别中的所有产品

这是我的Angular代码

{"id":1,"category":"cat","iconUrl":"www.test.com","subCategoryEntity":[{"id":3,"subCategory":"sub3","products":[]},{"id":2,"subCategory":"sub2","products":[]},{"id":1,"subCategory":"sub1","products":[{"id":1,"fileDownloadUri":"http://localhost:8081/api/v1/downloadFile/paint-bucket-orange-2-300x300.jpg","productName":"Name","productPrice":20.0,"productDesc":"this is the discreption","productStock":15,"productImages":[]}]}]}

这是输出 i can't access this object object

1 个答案:

答案 0 :(得分:-1)

要迭代所有子类别中的所有产品,您可以执行以下操作:

<div *ngFor="let subCategory of allProducts.subCategoryEntity">
   <div *ngFor="let product of subCategory.products">
      {{product.id}}-{{product.productName}}
   </div>   
</div>

我认为这是您想要的,这不是最好的方法。

我建议您遍历“ allProducts”和“ subCategories”并将产品提取到一个变量中,以仅执行一个“ ngFor”