简体   繁体   English

从指令中删除事件监听器-Angular.js

[英]Remove event listeners from directive - Angular.js

I have a problem with removing event listeners from mousewheel and wheel. 我从mousewheel和wheel删除事件侦听器时遇到问题。 I'm using fullpage.js plugin. 我正在使用fullpage.js插件。

The problem is, when I get from my "about page" to another by clicking link (I am using the ui router for angular.js) and then clicking on back button to get back on about page, the event listeners on mousewheel and wheel are not removed and get an additional listener attached. 问题是,当我从我的“关于页面”到另一个页面时,单击链接(我将ui路由器用于angular.js),然后单击“后退”按钮回到“关于页面”,鼠标滚轮和滚轮上的事件监听器不会被删除并附加一个附加的侦听器。 Repeating these steps add more listeners. 重复这些步骤将添加更多的侦听器。

.directive('dfAboutPagesView', function(  ) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
                scope.checkPackages = {
                    all: false
                };

            scope.$on("$destroy", function() {
                $(document).off();
            });

            var fullpage_in = function () {

                element.fullpage({
                    scrollingSpeed: 500,
                    autoScrolling: false
                });
                element.fullpage({
                    onLeave: function (index, nextIndex, direction) {
                        if (index == 1 && direction == 'down') {
                            $('#header').slideUp(500, 'easeInQuad');
                        }
                        else if (index == 2 && direction == 'up') {
                            $('#header').slideDown(500, 'easeInQuad');
                        }
                        else if (index == 3 && direction == 'down') {
                            $('#footer').slideDown(500, 'easeInQuad');
                        }
                        else if (index == 4 && direction == 'up') {
                            $('#footer').slideUp(500, 'easeInQuad');
                        }
                        }
                    });
                };
    fullpage_in();
            }
    }
})

Looking at the documentation for fullPage.js, I think you want to call the plugin's destroy method in your $destroy handler: 查看fullPage.js的文档,我认为您想在$destroy处理程序中调用插件的destroy方法:

scope.$on("$destroy", function() {
     $.fn.fullpage.destroy('all');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM