我想根据是否显示模式来禁用和启用Tab键事件。当我尝试启用TAB,但是从渲染器解除绑定时,我收到错误:
removeTabKeyListener不是函数...
export class ExampleComponent {
removeTabKeyListener: () => void;
ngOnInit(){
this.disableTab(true);
}
disableTab(allow: boolean) {
if(allow) {
this.removeTabKeyListener = this.renderer.listen('document', 'keydown', (event) => {
if (event.keyCode === 9) {
event.preventDefault();
}
});
}else {
this.removeTabKeyListener(); //ERROR HERE
}
}
}
答案 0 :(得分:1)
也许当您在代码中到达该行时,尚未为removeTabKeyListener
分配任何内容。
您可以尝试将removeTabKeyListener: () => void;
替换为removeTabKeyListener = () => {};
或添加如下所示的支票:
if (this.removeTabKeyListener) {
this.removeTabKeyListener();
this.removeTabKeyListener = null;
}