observable is not hit when there is a change in subject Angular 11

Viewed 31

I am trying to invoke the method from another component using behaviorSubect observable concept, onBroadcastToTeamChange is hitting everytime from component 1 but i am unable to subscribe to getTeamUId from component 2 as soon as change is detected in component 1.

shared service code : 
    
    import { Injectable } from '@angular/core';
    import { Subject, BehaviorSubject } from 'rxjs';
    
    @Injectable({
      providedIn: 'root'
    })
    export class ScopeSelectorService {
      public teamUId = new Subject<any>(); 
      getTeamUId = this.teamUId.asObservable(); 
      constructor() { }
      onBroadcastToTeamChange(tUId: any) {
        if (tUId != null) {
          this.teamUId.next(tUId);
        }
      }
    }
    
    component 1 in which there is a change:
    
      onChangeTeam(TeamUId: any) {
        this.scopeSelectorService.onBroadcastToTeamChange(TeamUId);
      }
    
    component 2 in which subscription is not working or hitting :
    
    onTeamChange(teamId: any): void {
      this.scopeSelectorService.getTeamUId.subscribe(data => {
        if (data != null) {
          this.getTeamData(data);
        }
      })
    }
1 Answers

Here is an example that works properly.

stackblitz

Also check the console

Related