1

当用户未接受协议等时,我想阻止某些路由。使用$locationChangeStart效果很好:

$scope.$on('$locationChangeStart', function(event, newVal, oldVal) {        
    var targetPage = newVal.substring(newVal.lastIndexOf("/") + 1);

    if (!acceptedRelease && targetPage != "") {
        event.preventDefault();
        $location.path("/");
    }
});

只要初始页面加载了路由/(即为空),它就可以正常工作。但是,如果您使用/mainMenu等加载页面,则它不起作用。路由本身被 阻止event.preventDefault(),但$location.path没有适当地触发/重定向。使用

$scope.$apply(function () { $location.path("/"); })

也不起作用。我得到一个错误$digest already in progress

阻塞路由后有什么方法可以重定向$locationChangeStart吗?

一般来说,有没有办法在 Angular 中阻止某些路由并在路由被阻止时执行重定向?

4

1 回答 1

-1

你可以在你的 routeChangeSuccess 中尝试这样的事情:

$rootScope.$on('$routeChangeSuccess',function(){
    if(!acceptedTermsVar){
      $location.path('/wherever-you-want');
    }
});
于 2013-11-18T21:22:40.193 回答