[英]Prevent Route Change Based on Routing Parameters in Angular
I've searched all over for this. 我已经到处搜寻了。 I basically want to be able to read the routing parameters collection and cancel the route if a certain parameter is missing.
我基本上希望能够读取路由参数集合并在缺少某个参数的情况下取消路由。 Using prevent default in $routeChangeStart does not work.
在$ routeChangeStart中使用prevent default不起作用。 It works in $locationChangeStart although this callback doesn't provide you with a routing parameters collection.
尽管此回调不为您提供路由参数集合,但它可以在$ locationChangeStart中工作。
Is there a way I can check for a routing parameter and if it is missing cancel the route? 有没有一种方法可以检查路由参数,如果缺少该参数,则取消路由?
$rootScope.$on('$locationChangeStart', function (event, next, current) {
// no route parameters... booo
});
$rootScope.$on('$routeChangeStart', function (event, next) {
if (!($.isNumeric(next.params.siteId) && +next.params.siteId)) {
event.preventDefault(); // this doesn't work
$location.url('home');
}
});
I've seen some hacks in other posts but they all seem specific to ngRoute. 我在其他文章中看到了一些技巧,但是它们似乎都是针对ngRoute的。 I am using angular routing segment though.
我正在使用角度路由段。
A bit old question but I just passed through the same situation and here is how I addressed: 有点老的问题了,但是我刚经历了同样的情况,这就是我的解决方法:
... ,onEnter: function($state, $stateParams) { if ($stateParams.id=='') { //or whatever constraint $state.go(<any other route>); return; } }, ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.