【发布时间】:2021-02-08 03:00:09
【问题描述】:
我正在尝试在 Angular 10 上的 HTML 表中显示数据,但收到此错误 Error trying to diff '[object Object]'. Only arrays and iterables are allowed。我在下面有这个 JSON:
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "petr4.SA",
"3. Last Refreshed": "2020-10-23",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2020-10-23": {
"1. open": "20.9400",
"2. high": "21.1400",
"3. low": "20.5400",
"4. close": "20.5700",
"5. volume": "61328500"
},
"2020-10-22": {
"1. open": "20.1000",
"2. high": "20.8400",
"3. low": "20.0500",
"4. close": "20.8400",
"5. volume": "98359400"
}
}
我的模型课:
export class Candle{
date: Date;
open: string;
high: string;
low: string;
close: string;
volume: string;
}
我的服务等级:
export class CandleService {
constructor(
public http: HttpClient
) { }
findAll(): Observable<Candle[]>{
return this.http.get<Candle[]>(`${API_CONFIG.baseUrl}`);
}
}
最后是 HTML:
<div class="container-analyzer">
<table>
<thead>
<th>Date</th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</thead>
<tbody *ngFor="let candle of ativoData">
<tr>
<td>{{candle.data}}</td>
<td>{{candle.open}}</td>
<td>{{candle.high}}</td>
<td>{{candle.low}}</td>
<td>{{candle.close}}</td>
<td>{{candle.volume}}</td>
</tr>
</tbody>
</table>
</div>
【问题讨论】:
-
这里的错误看起来很简单;您的数据既不是数组也不是可迭代的。
-
因为
ngFor中的“只允许数组和可迭代”