gpt4 book ai didi

angular - 尝试在 Angular 中区分 '[object Object]' 时出错

转载 作者:太空狗 更新时间:2023-10-29 16:46:33 26 4
gpt4 key购买 nike

看起来 HTML 中的 freight 变量有问题:

Error in app/freightList.component.html:13:8 Error trying to diff '[object Object]'

下面是代码

freight.service.ts
=======================

getFreight (): Promise<Freight[]> {
return this.http.get(this.freightUrl)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}

private extractData(res: Response) {
let body = res.json();
return body || { };
}

freightList.component.ts
========================
getFreight() {
this.freightService
.getFreight()
.then(freight => this.freight = freight)
.catch(error => this.error = error); // TODO: Display error message
}

freightList.component.html
============================

<tr *ngFor="let frght of freight">
<td>{{frght.grp}} - {{frght.grpname}}</td>
<td>{{frght.subgrp}} - {{frght.subgrpname}}</td>
<td>{{frght.prodno}} - {{frght.prodname}}</td>
<td>{{frght.percent}}</td>
<td>{{frght.effective_date}}</td>
<td><button (click)="deleteFreight(frght, $event)" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-remove"></span> Remove</button></td>
<td><button (click)="editFreight(frght)" class="btn btn-warning btn-sm"><span class="glyphicon glyphicon-edit"></span> Edit</button></td>
</tr>

Response body
==================

[{
"effective_date": "01/01/2016",
"grp": "01",
"grpname": "STOPS/FLEX HOSES/COVER PLATES",
"id": "1",
"percent": "10",
"prodname": "DWV PVC PIPE 100MM X 6MTR SN6 SWJ",
"prodno": "1400200",
"subgrp": "02",
"subgrpname": "DWV PIPE - UP TO 150MM"
}, {
"effective_date": "01/02/2016",
"grp": "01",
"grpname": "STOPS/FLEX HOSES/COVER PLATES",
"id": "2",
"percent": "11",
"prodname": "PVC PIPE 100MM X 6MTR SWJ SN10",
"prodno": "1400201",
"subgrp": "03",
"subgrpname": "DIMPLEX BATHROOM ACCESSORIES"
}]

最佳答案

您的 extractData(可能还有您的 HTTP API)正在返回一个对象 {} - ngFor 需要一个数组 [] 来迭代。

更改您的服务/API 以生成数组,或转换组件中的对象。

关于angular - 尝试在 Angular 中区分 '[object Object]' 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38216857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com