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