gpt4 book ai didi

javascript - 在所有 Controller 上触发的函数 - angularjs

转载 作者:行者123 更新时间:2023-11-28 17:56:55 24 4
gpt4 key购买 nike

我有这个

  $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";
}
}

在一个 Controller 中运行函数,但即使在我导航到另一页面后,scrollFunction也会被触发。如何将该功能仅限于特定页面( Controller )。

最佳答案

当 Controller 销毁时,您需要停止监听滚动事件

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

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

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

关于javascript - 在所有 Controller 上触发的函数 - angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44276947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com