Reputation: 83
I am absolute beginner in Angular2,
this are my components,how to update this.product Array in HeaderComponent , whenever buynow() is triggered in FoodDetailsComponent
export class HeaderComponent {
products: Array < any > ;
cart: Array < any > ;
Tabs: Array < any > ;
constructor(public ShopDataService: ShopDataService) {
this.products = this.ShopDataService.get();
}
ngOnInit() {}
}
export class FoodDetailsComponent {
@Input()
foodDetail: any;
constructor(private ShopDataService: ShopDataService) {
}
buynow(product) {
this.ShopDataService.add(product);
}
ngOnInit() {}
}
And i have few doubts how change Detection work,changeDetectionStrategy is only for child-parent component.how about sibling component works?
Upvotes: 1
Views: 583
Reputation: 124
you need to subscribe to the service when you get the data.
this.ShopDataService.get().subscribe( result => {this.products = result});
also please share the code in your service.
Upvotes: 1