[英]Display json data with angular 7
我想用 Angular 7 在屏幕上顯示 json 數據。我有一個錯誤。
找不到類型為“object”的不同支持對象“[object Object]”。 NgFor 僅支持綁定到 Iterables,例如 Arrays。
為我服務;
getData(): Observable <any> {
return this.http.get(this.myUrl);
}
在我的組件中;
public data: []= null;
ngOnInit() {
this.myService.getData().subscribe((d) => this.data = d);
}
在我的 html 中;
<tbody>
<tr *ngFor = "let people of data">
<td> {{ people.name}}</td>
</tr>
</tbody>
我的JSON;
{
"people": [
{
"number": [
0,
0
],
"name": "arya"
},
{
"number": [
1,
1
],
"name": "arya2"
}
]
}
我怎樣才能用 html 在屏幕上顯示這些數據,這些數據是 json 格式的?
使用空數組初始化變量data
。
public data: [] = [];
並將d.person
分配給data
變量。
this.myService.getData().subscribe((d) => this.data = d.people);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.