I am learning rxjs and I guess I am doing something wrong as my BehaviourSubject is emitting value only once.
In my Service -
private cartItemCount = new BehaviorSubject<Observable<Number>>(this.getCartItemsCount());
actualCartItemCount=this.cartItemCount.getValue();
getCartItemsCount(){
return this.http.get<Number>('/api/getCartItemsCount/'+this.getCartID());
}
Below Method is also in above service for Add or Remove Product but will be called from ProductCartComponent and inside I am calling Behaviour Subject next method
private updateProductCart(cartID:string,productId:number,change:string){
this.cartItemCount.next(this.getCartItemsCount());
return this.http.get<Item>('api/updateProductItem/'+cartID+'/'+productId+'/'+change);
}
Now in my NavBar Component I am subscribing to actualCartItemCount Observable
constructor( private cartService:ShopingCartService)
{ }
ngOnInit() {
this.cartService.actualCartItemCount.
subscribe(res => {this.totalCartItemCount = res;
}
In the console I can see getCartItemsCount will be called whenever add or remove is happening but in NavBar Component where I have subscribed to the actualCartItemCount is being called only once.
Kindly guide me and let me know if more details are required on this...
providers: [ShopingCartService]. - JB Nizetsubscriptionsomewhere like inngDestroyor something so that it is not getting - Sravanthis.cartItemCount.getValue()you have the returned value fromthis.getCartItemsCount()which is created only once. Then subscribingthis.cartService.actualCartItemCountis subscribing tothis.http.getand not theBehaviorSubject. - martin