Issue
Is it possible to remove listeners with the new angular 4 renderer?
here is the interface:
abstract listen(target: 'window' | 'document' | 'body' | any, eventName: string, callback: (event: any) => boolean | void): () => void;
In the renderer v1 listen and listenGlobal returns a Function, but this one returns void.
Is it an issue? If not, how can I remove a listener?
Solution
There is no difference with Renderer
:
import { Renderer2 } from '@angular/core';
export class MyComponent {
listenerFn: () => void;
constructor(private renderer: Renderer2) {}
ngOnInit() {
this.listenerFn = this.renderer.listen(document, 'mousemove', () => console.log('move'));
}
ngOnDestroy() {
if (this.listenerFn) {
this.listenerFn();
}
}
}
Answered By – yurzui
Answer Checked By – Senaida (AngularFixing Volunteer)