【发布时间】:2018-10-24 16:55:37
【问题描述】:
我从 GET REST 调用收到如下 JSON 对象。
[
{
"driverName": "Bob",
"customerName": "-",
"status": "AVAILABLE"
},
{
"driverName": "Harry",
"customerName": "-",
"status": "AVAILABLE"
},
{
"driverName": "Peter",
"customerName": "-",
"status": "AVAILABLE"
},
{
"driverName": "John",
"customerName": "-",
"status": "AVAILABLE"
},
{
"driverName": "Rob",
"customerName": "-",
"status": "AVAILABLE"
}
]
我想对此进行迭代并以表格格式显示它们。 我的代码如下。
AppComponent.ts
driverStatusResponse: any[] = [];
AppService.ts
getDriverStatus():Observable<any>{
var httpUrl = this.baseURL+"driverstatus/";
return this._http.get(httpUrl)
.pipe(map((response: Response) => <any> response.text()));
}
还有组件的html:
<tbody bgcolor="#b3ecff">
<tr *ngFor="let driver of driverStatusResponse">
<td>{{driver.driverName}}</td>
<td>{{driver.customerName}}</td>
<td>{{driver.status}}</td>
</tr>
</tbody>
我在控制台中收到如下错误:
AppComponent.html:24 ERROR Error: Error trying to diff '[{"driverName":"Bob","customerName":"kushhs","status":"BUSY"},{"driverName":"Harry","customerName":"aSdjb","status":"BUSY"},{"driverName":"Peter","customerName":"aSdjb","status":"BUSY"},{"driverName":"John","customerName":"zdgf","status":"BUSY"},{"driverName":"Rob","customerName":"asdfsg","status":"BUSY"}]'. Only arrays and iterables are allowed
at DefaultIterableDiffer.push../node_modules/@angular/core/fesm5/core.js.DefaultIterableDiffer.diff (core.js:6194)
at NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck (common.js:3386)
at checkAndUpdateDirectiveInline (core.js:10100)
at checkAndUpdateNodeInline (core.js:11363)
at checkAndUpdateNode (core.js:11325)
at debugCheckAndUpdateNode (core.js:11962)
at debugCheckDirectivesFn (core.js:11922)
at Object.eval [as updateDirectives] (AppComponent.html:35)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:11914)
at checkAndUpdateView (core.js:11307)
请帮我解决这个问题。
【问题讨论】:
-
您是从请求中检索 JSON 对象还是正在定义它?
-
我从请求中检索到它
标签: html json angular loops typescript