javascript Angularjs:捕获浏览器中的刷新事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23911850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:47:44  来源:igfitidea点击:

Angularjs: capture the refresh event in the browser

javascriptjqueryangularjs

提问by Tom

In angularjs is possible handle the user clicking the Refresh button on the browser? There is any method that the framework expose to the developer?

在 angularjs 中是否可以处理用户单击浏览器上的刷新按钮?框架有什么方法可以向开发人员公开吗?

Thanks

谢谢

回答by gtzinos

To handle the reload itself (which includes hitting F5) and to take action before it reloads or even cancel, use 'beforeunload' event.

要处理重新加载本身(包括按 F5)并在重新加载甚至取消之前采取行动,请使用 'beforeunload' 事件。

var windowElement = angular.element($window);
windowElement.on('beforeunload', function (event) {
   //Do Something

   //After this will prevent reload or navigating away.
   event.preventDefault();
});

回答by Yaroslav Osetrov

You can use $routeChageStart:

您可以使用$routeChageStart

 $rootScope.$on('$routeChangeStart', function () {
            alert('refresh');
        });