你好我在如何從rest API插入物件時遇到問題,我可以在控制臺日志中看到這些物件,但我無法將它系結到我的表的標簽中
這是我想在我的表中插入的物件
職員:姓名:“May Anne”[[Prototype]]:物件
這是我的 service.ts
//fetch discounte Bill
fetchDiscountedBill(): Observable<any>{
return this.http.get(this.baseUrl '/discountedBill');
}
這是model.ts
export class BillingModel{
clerk : {
name: string;
}
}
我用這個將 model.ts 呼叫到我的 table.component.ts
billingModel: BillingModel = {
clerk: {
name: ''
}
};
并用這個創建一個方法
getDiscountedBill(){
this.dataStorageService.fetchDiscountedBill().subscribe(resp => {
console.log(resp)
this.billingModel = data;;
}, err => {
console.log(err)
});
}
這是我要插入或系結物件的標簽
<tr>
<td colspan="4">Attending Clerk: </td>
</tr>
請幫忙謝謝!
uj5u.com熱心網友回復:
我認為您可以在組件方法中使用 this.BillingModel.clerk = resp.clerk
uj5u.com熱心網友回復:
在訂閱中的 ts 檔案中,您可以將值分配為:
this.billingModel.clerk = resp.clerk;
然后使用插值在您的 html 模板中顯示職員姓名:
{{billingModel?.clerk?.name}}
如果您的 HTTP 回應資料結構與您定義的模型匹配,那么您可以在服務類方法中使用與以下相同的型別:
fetchDiscountedBill(): Observable<BillingModel> {
return this.http.get<BillingModel>(this.baseUrl '/discountedBill');
}
在訂閱中,您可以簡單地執行以下操作:
this.billingModel = resp;
同樣在定義模型時,您不需要將其定義為 a class,但可以將其定義為介面:
export interface BillingModel {
clerk: {
name: string;
}
}
uj5u.com熱心網友回復:
我也有相同的問題資料顯示在控制臺中,但沒有顯示在表格中
并且認為在控制臺中顯示的陣列名稱是什么名稱寫在 ts 中,例如 res['name of array']
嘗試這個
getDiscountedBill(){
this.dataStorageService.fetchDiscountedBill().subscribe(resp => {
console.log(resp)
this.billingModel = res['orderList']; <== not work then try below ;
this.billingModel = res['clerk']; <== this will work
this.billingModel = res['clerk'].name; <== if you want to display name of clerk then try this
}, err => {
console.log(err)
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/428897.html
