简体   繁体   English

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

[英]Angularjs: capture the refresh event in the browser

In angularjs is possible handle the user clicking the Refresh button on the browser? 在angularjs中可以处理用户点击浏览器上的Refresh按钮吗? There is any method that the framework expose to the developer? 框架有哪些方法可以向开发人员公开?

Thanks 谢谢

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();
});

You can use $routeChageStart : 你可以使用$routeChageStart

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

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

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