Skip to content Skip to sidebar Skip to footer

Eventlistener In Every Controller

I have an Angular app use angular 1.7 and ui-router. In my index.html, I have document.addEventListener('keyup',onKeyupVT) - event never need remove. In every controller, I also ha

Solution 1:

If a controller adds a document wide event listener, it needs to remove the listener when the controller is destroyed.

app.controller("ctrl", function($document) {
    $document.on("keyup",onKeyup);
    functiononKeyup(ev) {
        console.log(ev);
    }

    //REMOVE event listeners
    this.$onDestroy = function () {
        $document.off("keyup",onKeyup);
    };
})

This is needed to prevent memory leaks and other unintended behavior.

Post a Comment for "Eventlistener In Every Controller"