Can AngularJS listen for $locationChangeSuccess on refresh?

后端 未结 2 699
春和景丽
春和景丽 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: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());
        });
    });
    

提交回复
热议问题