I know how to raise an event using EventEmitter. I can also attach a method to call if I have such a component:
<component-with-event (myevent)="mymethod($event)" />
When I have such a component, everything works fine. I have moved some logic to the service, and I need to raise the event from within the Service. I have done this:
export class MyService { myevent: EventEmitter = new EventEmitter(); someMethodThatWillRaiseEvent() { this.myevent.next({data: 'fun'}); } }
I have a component that should update some value based on this event, but I cannot get it to work. I tried this:
//Annotations... export class MyComponent { constructor(myService: MyService) { //myService is injected properly and i already use methods/shared data on this. myService.myevent.on(... // 'on' is not a method <-- not working myService.myevent.subscribe(.. // subscribe is not a method <-- not working } }
How to get MyComponent to subscribe to an event when the service that boosts it is not a component?
I'm on 2.0.0-alpha.28
EDIT: modified my “working example” to actually work, so focus can be placed on the non-working part;)
Code example: http://plnkr.co/edit/m1x62WoCHpKtx0uLNsIv
javascript angular
Per Hornshøj-Schierbeck Jun 30 '15 at 6:51 2015-06-30 06:51
source share