I want to update cart badge value (in the navbar component) everytime user click Add to Cart button (in the product-list component).
I have a cart button in navbar component (with a badge which have the value equal to the number of products being added in the cart)
navbar.html:
<nav class="navbar fixed-top navbar-light" style="background-color: rgb(243, 208, 8);">
<a class="navbar-brand store_title"> TeeRex Store </a>
<a class="navbar-brand mx-auto products" routerLink="/product-list" routerLinkActive="active_route"> Products </a>
<p-button class="nav-link ms-auto cart_btn" [badge]="cart_length" routerLink="/shopping-cart" icon="pi pi-shopping-cart" [raised]="true"></p-button>
</nav> <br>
product-list.ts:
addToCart(product: any) {
this._data.setShoppingCartList(product); // product is being added to the shopping list
}
I want to increment the badge value (in navbar) everytime user click this Add to Cart button. I just want to know how to update the value (cart_length) in the navbar component?
2
Answers
Assuming that you have two unrelated components you can use a shared service to achieve this.
create a shared service :
In your component A :
In component B :
If you want update or add, you can do like this
Service
Component A
Component B