Reputation: 2839
I have a system with the following structure (three components) :
Grand-Father -> Father -> Children
And a service : Service
which has a EventEmitter on it.
On all three components I have the service set as a provider, added it to the constructor and subscribed to the eventemitter:
this.subscription = this._service.eemiter.subscribe(data=> this.init(data));
Problem is, it only gets fired on the component that calls the method from the service which fires the event. My guess is that, by defining them this way each component get a different Service
and not the same one, therefore the event is only seen by the component that called the firing method.
What design should I make in order for all three components to be able to subscribe to the same service and actually catch the event?
Upvotes: 0
Views: 1145
Reputation: 658067
If you add it to the providers of every component, every component gets its own instance of the service (not shared).
Either add it to bootstrap(AppComponent, [MySharedService])
or to a common parent element.
The parent element with MyService
in the providers list is the root scope for the service instance. All children get the same instance (except another component in between also has MyService
in the providers list).
Upvotes: 2