AngularJS - Unbind $window of an event from specific directive

前端 未结 4 1073
甜味超标
甜味超标 2021-01-08 00:12

Inside one of my directives, I use angular.element($window).bind(\'scroll\'). When the directive is destroyed, I then want to unbind it. Normally,

4条回答
  •  日久生厌
    2021-01-08 00:37

    Just for more clearity on how it will goes in directive Within link function just use this make sure attach it to write element as in example it is attached to document

                    //attach event to document which is big thing
                    //make sure we remove it once we done with this
                    $document.on('keydown', handler);
    
                    //We need to remove this event as it should be only applicable when user is withing 
                    //the scope of directive parent
                    scope.$on("$destroy", function () {
                        $document.off('keydown', handler);
                    });
                    //function gets executed when we hit the key specified from form
                    function handler (event) {
                    }
    

提交回复
热议问题