当我尝试调用object.object []。方法中角度4的方法时出现以下错误:Angular TypeError:尝试调用object.object []方法时不是函数。方法
OrderComponent.html:30 ERROR TypeError: item.increaseQuantity is not a function at OrderComponent.increaseQuantity (order.component.ts:87)
我有的打字稿代码如下。当我调用OrderComponent.IncreaseQuantity(itemLoc:number)方法时,当我尝试在方法中调用this.orderItems [itemLoc] .increaseQuantity()方法时,它会生成上述错误。我不知道为什么它不知道OrderItem.increaseQuantity方法:
import { Component, Inject } from '@angular/core';
import { Http } from '@angular/http';
class OrderItem {
id: number;
name: string;
unitPrice: number;
quantity: number;
amount: number;
comment: string;
increaseQuantity(): boolean {
console.log("In the Order Item itself - worked.");
this.quantity += 1;
this.amount = this.quantity * this.unitPrice;
return false;
}
}
class Order {
id: number;
createdTime: Date;
orderType: string;
tableName: string;
orderItems: OrderItem[];
}
@Component({
selector: 'order',
templateUrl: './order.component.html'
})
export class OrderComponent {
public order: Order;
public total: number = 0;
constructor(http: Http, @Inject('ORIGIN_URL') originUrl: string) {
http.get(originUrl + '/api/Order/2').subscribe(result => {
this.order = result.json() as Order;
this.updateTotal();
});
}
updateTotal(): void {
this.total = 0;
//console.log("all: " + JSON.stringify(this.order.orderItems));
this.order.orderItems.forEach(s => this.total += s.amount);
}
increaseQuantity(itemLoc: number): boolean {
//item.increaseQuantity();
let item = this.order.orderItems[itemLoc] as OrderItem;
console.log("This increaseQuantity work." + JSON.stringify(item));
return item.increaseQuantity();
//return false;
}
}
谢谢你 - 解决问题。 – tazza