matchi
matchi

Reputation: 593

Listener is not a Function error when trying to unbind Renderer event listener

I want to disable and enable Tab key event based on whether a modal is shown or not. When I try to enable the TAB, but unbinding the event from the renderer, I got error:

removeTabKeyListener is not a function...

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 
        }
    }
}

Upvotes: 0

Views: 948

Answers (1)

IlyaSurmay
IlyaSurmay

Reputation: 2333

Perhaps when you reach that line in the code, nothing is assigned to removeTabKeyListener yet. You can try to replace removeTabKeyListener: () => void; with removeTabKeyListener = () => {}; or add check like this:

if (this.removeTabKeyListener) {
   this.removeTabKeyListener();
   this.removeTabKeyListener = null;
}

Upvotes: 1

Related Questions