0
votes

In Angular service i have created the subject and returning it as observable as follows :

  public entityPayLoad = new Subject<Entity>();

  public getSubjectAsObservable() {
    return this.entityPayLoad .asObservable();   

}

I am calling next method of subject from two different components as :

From Comp 1 :

 this.Service.entityPayLoad.next(this.entity1);

From Comp 2 :

this.Service.entityPayLoad.next(this.entity2);

I am subscribing to subject as follows which is getting called twice for one next method call:

this.entitiesSub = this.Service.getSubjectAsObservable().subscribe((entity) => {
            this.loadNewMainEntity(selectedMainEntity);
        });

Initially it works fine, but after getting two next method call, then for every subsequent one next request, subscriber gets called twice.

I have unsubscribed the same in ngondestroy. But as i am not leaving the component for subsequent next call, unsubscribe does not get called.

where do your service provided in root? - Asanka
@Asanka in provider section - Sagar K
yes in root (app.module) or in a sub module? - Asanka
can u use behavioralSubject instead of subject and check? - Asanka
@Asanka in app module - Sagar K