I have the following implementation of the directive. How to remove the EventListener 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(){
document.removeEventListener('keyup', enter, false);
}
}
I know that I can just use the global variable here and access it. But I do not want to save the state of the instance in a global variable.
user6895247
source
share