davidvera
davidvera

Reputation: 1499

Angular 6 : passing data from components and push objects in array

I created 2 components, one with products and one with shopping cart... They communicate through a service. Each time I add a product, this product is replaced by the new one ... Technically I would like to "push" the product in an array of products

cart: Product[];
product : Product;
subscription : Subscription;


constructor(private basketdataService: BasketdataService) {
    this.subscription = this.basketdataService.onGetBasket().subscribe(
      product => {
        this.product = product;
      });
    console.log("résultat");
}

In fact, I wonder how I can push data in this array, avoiding to make it empty at each event.

Upvotes: 0

Views: 92

Answers (1)

Teun van der Wijst
Teun van der Wijst

Reputation: 1029

this.subscription = this.basketdataService.onGetBasket().subscribe(
  product => {
    this.cart.push(product);
  });

would that work for you?

Upvotes: 1

Related Questions