服务:
getGroups():Observable<Group[]> {
return this._http.get(this.url).map((res: Response) => res.json());
}
类别:
export class Group {
id: number;
name: string;
vat: string;
items: Item[];
addItem = function(item: Item): void{
this.items.push(item);
}
}
组件:
groups:Group[];
selectedGroup:Group;
onSelectGroup(currentGroup:Group):void {
this.selectedGroup = currentGroup;
}
ngOnInit():void {
this.groupService.getGroups().subscribe(groups => this.groups = groups);
}
模板:
<div *ngFor="let g of groups" [class.selected]="g === selectedGroup">
<button (click)="onSelectGroup(g)">Select group</button>
</div>
当我尝试使用该方法(在单个数组元素 - 当然是组)addItem(ItemObject)时,它说“this.selectedGroup.addItem不是函数”。
请帮助