角度路由声明参数的提供程序集



是否可以使用 angularjs 只允许 routeProvider 中的某些选择?

例如,如果我有:

$routeProvider.when('/:action/view/', {

我只能允许("篮球"、"棒球")作为操作将其设置为比赛吗?

您可以使用路由的重定向到属性在不允许路由调用的情况下重定向用户:

$routeProvider.when('/:action/view/',{
    redirectTo: function(routeParams, locationPath, locationSearch){
        var allowed = ['baseball','basketball'];        //allowed action param
        if(allowed.indexOf(routeParams.action) != -1){  //route is allowed don't redirect
            return null;
        }
        else{                                           //route is denied redirect to home
            return '/home';
        }
    }
}

未测试,但您可以监听在路由更改之前调用的$routeChangeStart,使用"下一个"和"当前"路由参数来确定是否允许未来路由。如果没有,请执行 $location.path 以将它们设置回当前路由本身。

最新更新