-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
  • 107,083
  • 22
  • 201
  • 376
Peter Horton
  • 138
  • 8

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
  • 452
  • 3
  • 6