如何在不配置路由的情况下检测angularjs单页面应用程序中的页面刷新?

时间:2015-01-16 12:20:00

标签: javascript angularjs

我需要在单页面应用程序中检测页面刷新事件。

1 个答案:

答案 0 :(得分:36)

您无法确定,因为浏览器页面刷新相当于退出应用程序然后再次打开它。

您可以做的是检测页面退出事件和应用程序打开事件。

在app.run()块中注入'window'依赖项并添加:

window.onbeforeunload = function () {
   // handle the exit event
};

和$ routeChangeStart事件

$rootScope.$on('$routeChangeStart', function (event, next, current) {
  if (!current) {
    // handle session start event
  }
});

如果肯定你需要链接这两个事件,它必须在后端/服务器端....

我希望这会有所帮助。

相关问题