Angularjs: captura el evento de actualización en el navegador

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?

Muchas Gracias

preguntado el 28 de mayo de 14 a las 13:05

Not sure what you intend to "handle". But a quick Google search lists the following discussions: 1. Info on Angular $routeProvider: stackoverflow.com/questions/17324902/… 2. Disabling refreshes: stackoverflow.com/questions/2482059/… 3. Handle route events: stackoverflow.com/questions/16344223/… -

2 Respuestas

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();
});

Respondido 31 ago 16, 10:08

Puedes usar $routeChageStart:

 $rootScope.$on('$routeChangeStart', function () {
            alert('refresh');
        });

contestado el 28 de mayo de 14 a las 14:05

As far as I know, this is not what OP asked for. This event fires every time the view is changed, not specially on user refresh. - sebastialonso

Though the answer is wrong, it does not deserve a negative penalty - Siva Tumma

I don't think this would work; the beforeunload is pretty tough on accepting changes in behaviour from javascript. e.g. with angular 7 the de algun modo equivalent approach with canDeactivate no funciona. - Adrian

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.