简体   繁体   中英

Angular - subject observable not responding

I have one service SelectorService where I have defined the subject and an observable.

private eventEligibleApiStatus = new Subject<any>();
public eventEligibleApiStatusUpdated$ = this.eventEligibleApiStatus.asObservable();

And I have defined a method which calls the subject.next.

notifyEventEligibleApiStatus(status) {
  this.eventEligibleApiStatus.next(status);
}

Now in another component called scheduler-component.ts, I have defined one boolean flag and an observable

  eventEligibleInProgress = false;

  eventEligibleListener: Observable<any> = this.resourceSelectorService.eventEligibleApiStatusUpdated$;

And I am subscribing to this observable

   this.eventEligibleListener.subscribe( status => {
     this.eventEligibleInProgress = status;
   });

From SelectorComponent, I am calling the SelectorService's, notifyEventEligibleApiStatus like this,

this.selectorService.notifyEventEligibleApiStatus(true);

whenever the notifyEventEligibleApiStatus function is triggered, the control does not come to the eventEligibleListener.subscribe and the flag is also not changed.

What am I doing wrong here.

PS: I am new to Angular and Front End development.

May be SelectorService is not SingleTon and creating different object for each component. One way you can try @Injectable({ providedIn: 'root' }) this will create SingleTon object and same object will be shared across the components.

You have not declared eventEligibleApiStatus subject in your code. You have shared bits of codes. Try sharing complete code so that we can have a better understaning instead of guessing.

Try using behaviourSubject

private eventEligibleApiStatus = new BehaviorSubject<any>(null);
public eventEligibleApiStatusUpdated$ = this.eventEligibleApiStatus.asObservable();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM