Service:
getGroups():Observable<Group[]> {
return this._http.get(this.url).map((res: Response) => res.json());
}
Class:
export class Group {
id: number;
name: string;
vat: string;
items: Item[];
addItem = function(item: Item): void{
this.items.push(item);
}
}
Component:
groups:Group[];
selectedGroup:Group;
onSelectGroup(currentGroup:Group):void {
this.selectedGroup = currentGroup;
}
ngOnInit():void {
this.groupService.getGroups().subscribe(groups => this.groups = groups);
}
Template:
<div *ngFor="let g of groups" [class.selected]="g === selectedGroup">
<button (click)="onSelectGroup(g)">Select group</button>
</div>
When I try to use the method (on a single array element - group of course) addItem(ItemObject) it says "this.selectedGroup.addItem is not a function".
Help please