阻止路线的角度重定向

时间:2013-11-18 17:07:23

标签: javascript angularjs angular-routing

我想在用户未接受协议等时阻止某些路由。使用$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中的某些路由并在路由被阻止时执行重定向?

1 个答案:

答案 0 :(得分:-1)

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

$rootScope.$on('$routeChangeSuccess',function(){
    if(!acceptedTermsVar){
      $location.path('/wherever-you-want');
    }
});
相关问题