Hari Das
Hari Das

Reputation: 10864

Why RxJS Subject cannot access latest data in angular component?

Here is how I am communicating between two unrelated component. Component B is trying to access myObj from component A. This is strange that this.myObj is not always pointing to latest data. Any reason why ?

component A {
      myService.pubSub.subscribe((data)=>{
        if(data.type == "getMyObj"){
            myService.pubSub.next(this.myObj);
        }
      });
}

component B {
     myService.pubSub.next({type: "getMyObj"});
}

Where myService.pubSub is just a subject in a service.

Upvotes: 0

Views: 43

Answers (1)

Hari Das
Hari Das

Reputation: 10864

I got the answer. The issue was that, I was not unsubscribing to the subject and there were multiple subscriptions. So once I did unsubscribe, it solved the issue.

sub.unsubscribe();

Upvotes: 1

Related Questions