Javascript 如何在不配置路由的情况下检测 angularjs 单页应用程序中的页面刷新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27983830/
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
How can I detect a page refresh in an angularjs single page application without configuring routes?
提问by Mudits
I need to detect a page refresh event in my single page application.
我需要在我的单页应用程序中检测页面刷新事件。
回答by christina
You can't for sure, because a browser page refresh is equivalent to exiting the app then opening it again.
您不能确定,因为浏览器页面刷新相当于退出应用程序然后再次打开它。
What you can do is detect the page exit event and the app open event.
您可以做的是检测页面退出事件和应用程序打开事件。
In the app.run() block inject 'window' dependency and add:
在 app.run() 块中注入 'window' 依赖项并添加:
window.onbeforeunload = function () {
// handle the exit event
};
and on $routeChangeStart event
和 $routeChangeStart 事件
$rootScope.$on('$routeChangeStart', function (event, next, current) {
if (!current) {
// handle session start event
}
});
And if definitely you need to link these two events it has to be on the backend/server side ....
如果你确实需要链接这两个事件,它必须在后端/服务器端......
I hope this helps.
我希望这有帮助。

