In my project, I have a service set up like this:
@Injectable({
providedIn: 'root'
})
export class MyService {
private mySubject = new BehaviorSubject({});
public currentData = this.mySubject.asObservable();
updateData(data: any) {
this.mySubject.next(data);
}
}
There is also a component that subscribes to the Observable
:
export class MyComponent implements OnInit {
constructor(private myService: MyService) { }
ngOnInit() {
this.myService.currentData.subscribe(data => /* some logic here */);
}
}
Additionally, there is another component that calls the updateData
method of the service.
export class AnotherComponent {
constructor(private myService: MyService) { }
onClick(data) {
this.myService.updateData(data);
}
}
Although I expected the .subscribe
in MyComponent
to be triggered when this.myService.updateData
is called in AnotherComponent.onClick
, it does not. However, I can confirm that the Observable has a subscriber attached to it, as the code indicates.