Can AngularJS listen for $locationChangeSuccess on refresh?

后端 未结 2 700
春和景丽
春和景丽 2021-01-03 21:36

I am listening for $locationChangeSuccess in Angular using this code:

$scope.$on(\'$locationChangeSuccess\', function(event) {
  console.log(\'C         


        
相关标签:
2条回答
  • 2021-01-03 22:19

    You probably want a listener on window.onbeforeunload which is called just before the window unloads it's resources (refresh).

    0 讨论(0)
  • 2021-01-03 22:38

    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());
        });
    });
    
    0 讨论(0)
提交回复
热议问题