-2

Here is my code:

App.component

ngOnInit() {
    this.favoritesServ.addFavoriteEvent
      .subscribe(data => this.name = data)
}

Service

addFavoriteEvent = new EventEmitter();
pushData(value) {
  this.addFavoriteEvent.emit(value)
}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Peter Horton
  • 148
  • 9

1 Answers1

-2

Use an Observable or Promise for your purpose.

Also, according to Angular2 style guide, don't abbreviate class names or properties.

A very simple example would be:

Service:

private observer; //an observer that 'emits' values to the Observable 
addFavoriteEvent : Observable<any> = new Observable<any>(observer => this.observer = observer); 
//create an instance of an Observable and assign the created observer to our local observer for easy re-usage.


pushData(value) {

  this.observer.next(value);
}

Your component may stay unchanged.

lastWhisper
  • 462
  • 3
  • 6