Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to detect a page refresh event in my single page application.

like image 254
Mudits Avatar asked Jan 16 '15 12:01

Mudits


People also ask

How to detect page refresh in AngularJS?

What you can do is detect the page exit event and the app open event. $rootScope. $on('$routeChangeStart', function (event, next, current) { if (! current) { // handle session start event } });

What happens when we refresh a page in Angular?

From then on, the Angular framework kick in and loads the appropriate modules and pages as route changes. When you refresh the page you lose all the state of the application since you are reloading the index. html with all the required dependencies again.

How can I tell if ASP NET is refreshing a page?

'IsPostback' will always have the value which was set previously. So for instance if the page was posted back before refresh, then the value will be true and if the page is not posted back before refresh, then the value will be false.

How does AngularJS detect browser?

There is a library ng-device-detector which makes detecting entities like browser, os easy. Inject "ng. deviceDetector" as dependency in your module. Then inject "deviceDetector" service provided by the library into your controller or factory where ever you want the data.

What is a browser refresh in angular?

An Angular application is a single page application, and in the context of a single page application the full app is loaded once, and data or screens are refreshed as the user uses your application. A browser refresh is the equivalent of shutting down your application and launching it again.

How to reload entire page using router in angular?

One is to use window.reload to reload entire page, and the other is to use onSameUrlNavigation-reload refresh component with an angle router. Some times, As a developer, you need to write a logic to reload a component or a page for a below cases In Angular, page navigation can be done with or without angular router module

How to detect a page load event in angular?

There are many other ways of detecting a page load event in Angular. The constructor of your AppModule. A service injected into the root that is used at least once. There is also the main.ts file which bootstraps the Angular application. Basically, anywhere there is JavaScript that runs once...

What is the use of window in angular?

As window is an global object which can be reused directly in Angular components. window.location.reload () loads current page reloadCurrentPage () { window.location.reload (); } Moving from one page to another in a legacy web application reloads the page and its content.


1 Answers

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.

like image 92
Christina Avatar answered Oct 16 '22 10:10

Christina