I have a service:
import { EventEmitter, Injectable } from '@angular/core';
@Injectable()
export class PingOnActionListenerService {
removingListener = new EventEmitter();
removeListener(): void {
this.removingListener.emit();
}
At component A I call the service:
this._pingOnActionListenerService.removeListener();
At component B I want to listen to the service:
ngOnInit() {
this._pingOnActionListenerService.removingListener.subscribe(this.deactivatelistener());
}
deactivatelistener() {
window.removeEventListener('click', this.pingIfLastPingIsOld);
}
When I run this code, I get at the console the error message:
core.js:1671 ERROR TypeError: generatorOrNext is not a function at SafeSubscriber.schedulerFn [as _next] (core.js:3565) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:195) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:133) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54) at EventEmitter.push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next (Subject.js:47) at EventEmitter.push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit (core.js:3537) at PingOnActionListenerService.push../src/app/core/services/ping-on-action/ping-on-action-listener.service.ts.PingOnActionListenerService.removeListener (ping-on-action-listener.service.ts:19) at CatchSubscriber.selector (logging-interceptor.ts:45) at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error (catchError.js:33)
this.deactivatelistener()
returns undefined, so you're passingundefined
to subscribe – user184994this._pingOnActionListenerService.removingListener.subscribe(this.deactivatelistener());
you are not calling the functionremovingListener
here. – callbackremovingListener
is not a function, it's anEventEmitter
– user184994subscribe(this.deactivatelistener());
.. it should besubscribe(this.deactivatelistener)
orsubscribe(() => this.deactivatelistener());
– callback