Difference between EventEmitter.next() and EventEmitter.emit() in Angular 2

AngularAngular2 Services

Angular Problem Overview


What is the difference between EventEmitter.emit() and EventEmitter.next()? Both dispatching the event to the subscribed listeners.

export class MyService {
  @Output() someEvent$: EventEmitter<any> = new EventEmitter();

  someFunc() {
   this.someEvent$.emit({myObj: true});

   this.someEvent$.next({myObj: true});
  }
}

The documenation for the EventEmitter is not so helpful at the moment.

Angular Solutions


Solution 1 - Angular

Solution 2 - Angular

In the latest version(Ng9), the source code of event_emitter.ts goes as following:

export class EventEmitter<T extends any> extends Subject<T> {
  /**
   * Emits an event containing a given value.
   * @param value The value to emit.
   */
  emit(value?: T) { super.next(value); }
}

EventEmitter extends from parent class Subject. And emit method call super.next() as you may expected.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHolger StitzView Question on Stackoverflow
Solution 1 - AngularGünter ZöchbauerView Answer on Stackoverflow
Solution 2 - AngularChris BaoView Answer on Stackoverflow