简体   繁体   中英

How can I detect a page refresh in an angularjs single page application without configuring routes?

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

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:

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

and on $routeChangeStart event

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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