我想在用户未接受协议等时阻止某些路由。使用$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中的某些路由并在路由被阻止时执行重定向?
答案 0 :(得分:-1)
你可以在routeChangeSuccess中尝试这样的事情:
$rootScope.$on('$routeChangeSuccess',function(){
if(!acceptedTermsVar){
$location.path('/wherever-you-want');
}
});