AngularJS - Need some combination of $routeChangeStart and $locationChangeStart

后端 未结 4 1761
夕颜
夕颜 2020-12-23 10:46

My problem is actually very similar to the one found here:

AngularJs - cancel route change event

In short, I\'m using $routeChangeStart and trying to change

4条回答
  •  悲哀的现实
    2020-12-23 11:12

    Since version 1.3.0 you can actually use the newly introduced preventDefault-method. With that you can cancel the current route change and then apply your own custom redirect as shown in this github-issue:

    
    $rootScope.$on("$routeChangeStart", function (event, next, current) {
        if (next.access) {
          event.preventDefault();
          $rootScope.$evalAsync(function() {
            $location.path('/login');
          });
        }
    });
    

    I implemented this method in my own project and it works perfectly. Hope it helps anyone else who stumbles across it.

提交回复
热议问题