In angularjs is possible handle the user clicking the Refresh button on the browser? There is any method that the framework expose to the developer?
Thanks
If you want to detect the browser refresh with Angular, i will show you a simple way to do it. You need to look at the Angular Router. Specifically to the navigated property. This property is false when the router starts and after the first navigation it changes to true .
Each time the resolver is resolved is equal to a page refresh in the browser. const routes: Routes = [ { path: '', component: TopComponent, resolve: { loaded: PageLoadedResolver }, children: [ ... ] } ]; There are many other ways of detecting a page load event in Angular. The constructor of your AppModule .
we will use window keyup and window keydown event to prevent browser page refresh in angular app. You can easily prevent browser refresh in angular 7, angular 8, angular 8 and angular 9 application. Now you can run and check it.
To handle the reload itself (which includes hitting F5) and to take action before it reloads or even cancel, use 'beforeunload' event.
var windowElement = angular.element($window);
windowElement.on('beforeunload', function (event) {
//Do Something
//After this will prevent reload or navigating away.
event.preventDefault();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With