Here is my implementation of a directive in Angular. I am facing an issue with removing the event listener in this case:
import { Directive, ElementRef, OnDestroy } from "@angular/core";
@Directive({
selector: "[Enter]"
})
export class Enter implements OnDestroy{
constructor(el: ElementRef) {
let enter = function(event){
if(event.keyCode === 13){
el.nativeElement.click();
}
}
document.addEventListener('keyup', enter , false);
}
ngOnDestroy(){
//How can I remove the event listener properly here?
}
}
I understand that using a global variable to access 'enter' function would solve the issue, but I prefer not storing the state of instance in a global variable.