簡體   English   中英

如何在Angular 4中打印json數組?

[英]How can I print json array in Angular 4?

這是json的樣子:

--------------------------------------------------
    "id": 2,
        "user": {
            "id": 1,
            "name": "User",
            "surname": "User",
            "email": "user@example.com"
        },
        "host": {
            "id": 1,
            "name": "Azza"
        },
        "products": [
            {
                "id": 3,
                "name": "Desert Tiramisu",
                "price": "2.40",
                "media_ids": "3,4",
                "quantity": 1
            }
        ],
-------------------------------------------------

HTML部分:

  <div *ngFor="let item of active_orders">
    <div class="row">
      <div class="col-md-12">
        <div class="col-md-4">
          <h4>
            {{item.products.name}}
          </h4>
        </div>
      </div>
    </div>
  </div>

我要打印產品名稱(例如在我們的案例中為“沙漠提​​拉米蘇”)。 但是我沒有任何結果。 我的錯誤在哪里?

您需要嵌套ngFor,因為產品是一個數組

  <div *ngFor="let item of active_orders">
    <div class="row">
      <div class="col-md-12">
        <div class="col-md-4"  *ngFor="let detail of item.products">
          <h4>
            {{detail.name}}
          </h4>
        </div>
      </div>
    </div>
  </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM