I'm trying to remove all my event listeners when route changes with ngOnDestroy, but I couldn't find a way to do it.
The callbacks in each listener is a method from my component with 'this' binded.
My code looks like this at the moment:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-partners-promote-geolocation',
templateUrl: './partners-promote-geolocation.component.html'
})
export class PartnersPromoteGeolocationComponent implements OnInit {
constructor () {}
ngOnInit() {
document.addEventListener('scroll', this.infoWindowPositionState.bind(this))
document.addEventListener('click', this.infoWindowVisibilityState.bind(this))
}
ngOnDestroy () {
document.removeEventListener('scroll', this.infoWindowPositionState.bind(this))
document.removeEventListener('click', this.infoWindowVisibilityState.bind(this))
}
infoWindowPositionState () {
(...)
}
infoWindowVisibilityState () {
(...)
}
}
I can add the event listeners normally, but when the route changes those events aren't removed. In my React projects this works like a charm with componentDidMount and componentWillUnmount, and I can prevent memory leaks, but I'm not succeeding to accomplish this in angular.
How could I do this in Angular 5?
Thanks!