1

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?

5
  • show your html, how these components are related. and where do you use onPush Commented Jul 13, 2017 at 5:06
  • this is main html,<div class="container"> <nbits-header></nbits-header> <router-outlet></router-outlet> </div> Commented Jul 13, 2017 at 5:19
  • 1
    read Everything you need to know about change detection in Angular to understand how onPush works Commented Jul 13, 2017 at 5:20
  • try like this public changedetect: ChangeDetectorRef this.changedetect.detectChanges() Commented Jul 13, 2017 at 5:31
  • this main html, <div class="container"> <shopkart-header></shopkart-header> <router-outlet></router-outlet> </div> shopkartHeader html , <div class="navbar-header"> <button type="button" class="navbar-toggle"> <span>{{products.length}}</span> </button> </div> in router-outlet, <div class="col-md-3" *ngFor="let food of foodDetail"> <div> <button class="btn btn-primary" (click) = "buynow(food)">Buy Now!</button> </div> </div> Commented Jul 13, 2017 at 5:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.