Angular 2 document.removeEventListener doesn't work in class
You have to specify the same function to removeEventListener
as you provided to addEventListener
. The function returned by bind
is not the same as the original function (if it were, it would have the this
issue).
So you'll have to store your bound functions and use them when calling removeEventListener
.
initResize(e): void {
this.mouseX = e.clientX;
this.mouseY = e.clientY;
if (!this.onResizeBound) {
this.onResizeBound = this.onResize.bind(this);
}
if (!this.stopResizeBound) {
this.stopResizeBound = this.stopResize.bind(this);
}
document.addEventListener('mousemove', this.onResizeBound, false);
document.addEventListener('mouseup', this.stopResizeBound, false);
}
and
stopResize(e): void {
document.removeEventListener('mousemove', this.onResizeBound, false);
document.removeEventListener('mouseup', this.stopResizeBound, false);
}