Javascript 在所有控制器上触发的函数-angualrjs

Javascript 在所有控制器上触发的函数-angualrjs,javascript,angularjs,scrolltop,document-body,Javascript,Angularjs,Scrolltop,Document Body,我有这个 $window.onscroll = function() { scrollFunction() }; function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { document.getElementById("btt"

我有这个

  $window.onscroll = function() { scrollFunction() };

            function scrollFunction() {
                if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
                    document.getElementById("btt").style.display = "block";
                } else {
                    document.getElementById("btt").style.display = "none";
                }
            }

函数,但即使在我导航到另一个页面后,Scroll函数也会被激发。如何将该功能仅限于特定的UALR页面(控制器)。

当控制器销毁时,您需要停止侦听滚动事件

angular.element($window).on('scroll', scrollFunction);

scope.$on('$destroy', function () {

  angular.element($window).off('scroll', scrollFunction);
});

当控制器销毁时,您需要停止侦听滚动事件

angular.element($window).on('scroll', scrollFunction);

scope.$on('$destroy', function () {

  angular.element($window).off('scroll', scrollFunction);
});

是用angularjs还是JavaScript(angularjs@RameshRajendran但您的代码看起来像JavaScript。将此脚本放在一个控制器中,并在滚动页面中使用此控制器。@RameshRajendran除了
$window
:)之外的所有内容都是angularjs还是JavaScript?:(angularjs@RameshRajendran但您的代码看起来像JavaScript。将此脚本放在一个控制器中,并在滚动页面中使用此控制器。@RameshRajendran除了
$window
之外的所有内容:)