Can AngularJS listen for $locationChangeSuccess on refresh?

angularjs, javascript

Solution

You can't register from within a controller because $locationChangeSuccess occurs before the route is matched and the controller invoked. By the time you register, the event has already been fired.

However, you can subscribe to the event on $rootScope during the application bootstrap phase:

var app = angular.module('app', []);
app.run(function ($rootScope) {
    $rootScope.$on('$locationChangeSuccess', function () {
        console.log('$locationChangeSuccess changed!', new Date());
    });
});

Problem

I am listening for $`locationChangeSuccess` in Angular using this code: ``` $scope.$on('$locationChangeSuccess', function(event) { console.log('Check, 1, 2!'); }); ``` However, it will only log in the console that the location has changed when I navigate to a new link. Naturally, this makes sense. My question is, how can I make Angular listen for `$locationChangeSuccess` in the event that I refresh the page?

Original source