I am trying to use the "Subject" class in angular2 to broadcast an event as an observable, from a service to all the components that have the instance of that service in them.
in my service, I have the following:
private loggedIn = new Subject<boolean>();
isUserLoggedIn(): Observable<boolean>
{
return this.loggedIn.asObservable();
}
test()
{
this.loggedIn.next(true);
}
And then in the component that uses this service, I have (_auth is the same service defined above):
ngOnInit()
{
console.log("yay!");
this._auth.isUserLoggedIn()
.subscribe(
d => {
console.log(d);
},
d => {
console.log(d);
},
() => {
console.log("Done!");
}
);
}
Now the issue is if I call the service's method this._auth.test();
from within this same component, everything works fine and the console.log lines get executed in this component. But if the service's method is called from another component, this component does not get triggered to run the console.log lines.
How can I make sure that the code in this component runs even if the service's method is triggered by another component? (Assume both components are rendered at the same time).
Thanks!