Getting response from the behaviourSubject everytime even after unsubscribing

43 views Asked by At

I am passing data from one component to another, but when I leave the component the subscription should get unsubscribed but it is not.

service:

sendDueDatetoPayments = new BehaviorSubject<any>(null);

  communicateDueDatetoPayments(msg) {
    this.sendDueDatetoPayments.next(msg);
  }

sending component:

dialogRef.afterClosed().subscribe(confirm => {
  if (confirm) {
    this.sharedService.communicateDueDatetoPayments(this.dueDateUpdatedTransctions); //passing data
  }
});

Receiving component:

dueDateSubscription: any;


ngOnInit(){
 this.listenDueDateRes();
}


listenDueDateRes() {
    this.dueDateSubscription = this.sharedService.sendDueDatetoPayments.subscribe((res: any) => {
      console.log(res)
      if (res) {
       //do something after getting the response
      }
    });
  }

ngOnDestroy(){
    this.dueDateSubscription.unsubscribe();
}
0

There are 0 answers