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

2

144

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.

Alcot answered 7/3, 2016 at 9:51 Comment(0)
A
172

They do the same. emit() is the current version, next() is deprecated.

See also https://github.com/angular/angular/blob/b5b6ece65a96f5b8f134ad4899b56bf84afe3ba0/modules/angular2/src/facade/async.dart#L49

Antebellum answered 7/3, 2016 at 9:52 Comment(0)
H
18

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.

Harl answered 21/4, 2020 at 1:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.