簡體   English   中英

如何在角度中使用* ngFor遍歷“帶有數組的對象”?

[英]How to iterate through “Object carrying an array” using *ngFor in angular?

我正在嘗試遍歷包含數組的對象,但收到錯誤消息“找不到類型為'object'的其他支持對象'[object Object]'。NgFor僅支持綁定到Iterables,例如Arrays。” 以下是我的json響應:

data={"books": [{"title": "A","edition": 3,"year": 2011 },
{"title": "B","edition": 2,"year": 2009},
{"title": "C","edition": 2,"year": 2008}],
"count":3
}

模板代碼:

<tr *ngFor="let d of data">
<td>d.books.title</td>
</tr>

誰能建議需要進行哪些更改才能遍歷數據?

您需要使用數組,即data.books

<tr *ngFor="let d of data.books">
<td>{{d.title}}</td> //edited after looking at comments
</tr>
In your .ts file is : Declare the data object
  data =
      {"books": 
      [
        {"title": "A","edition": 3,"year": 2011 },
        {"title": "B","edition": 2,"year": 2009},
        {"title": "C","edition": 2,"year": 2008}
      ],
        "count":3
      }

In your .html file:

<table>
    <tr *ngFor="let d of data.books">
     <td>{{d.title}}</td>
    </tr>
</table>

我嘗試了。。它對我有用! 在此處輸入圖片說明

暫無
暫無

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

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