Angularjs Angular$routeProvider为命名组(regex?)指定有效值

Angularjs Angular$routeProvider为命名组(regex?)指定有效值,angularjs,route-provider,Angularjs,Route Provider,我的问题是,如果第一个path参数是有效的“排序”值,那么应该加载一个控制器 /date /name 否则它应该加载我的错误页面 我的routeProvider条目如下所示: $routeProvider. when('/:sort', { controller: 'myCtrl' }) .otherwise({ templateUrl: 'error.html' }); 我的问题

我的问题是,如果第一个path参数是有效的“排序”值,那么应该加载一个控制器

/date
/name
否则它应该加载我的错误页面

我的routeProvider条目如下所示:

$routeProvider.
        when('/:sort', {
            controller: 'myCtrl'
        })
        .otherwise({
            templateUrl: 'error.html'
        });
我的问题是,无论给定什么值,控制器都将被加载

'/name' loads the controller so does '/asdf'
/asdf应加载错误页面

我的问题是,是否有任何方法可以为命名组指定有效值(可能使用regex),或者我必须指定多个routeProvider条目,例如

$routeProvider.
        when('/date', {
            controller: 'myCtrl'
        })
        when('/name', {
            controller: 'myCtrl'
        })
        .otherwise({
            templateUrl: 'error.html'
        });
如果这是唯一的解决方案,请跟进问题:

在我的控制器中,我使用routeParams检索:sort的值,这非常方便

如果我不使用命名组,而是为每个有效的排序值使用$routeProvider条目,则$routeParams中不会包含sort的值

然后我必须解析路径以获得正确的排序值,还是有更直接的方法


谢谢。

$routeChangeStart是您需要的

只要看一下文档,只要路线发生变化,就会发生此事件

$rootScope.$on("$routeChangeStart", function (event, next, current) {
  //next is the next route information and current the current route.

 // if regex.dontMatch(next) then redirect else don't do anything
});
其中regex.dontMatch是您的regex函数