我的问题是,如果第一个路径参数是有效的"排序"值,则应加载该控制器
,例如/date
/name
否则它应该加载我的错误页面。
我的路由提供程序条目如下所示:
$routeProvider.
when('/:sort', {
controller: 'myCtrl'
})
.otherwise({
templateUrl: 'error.html'
});
我的问题是无论给出什么值,控制器都会被加载
'/name' loads the controller so does '/asdf'
/asdf 应加载错误页面。
我的问题是否有任何方法可以为命名组指定有效值(可能使用正则表达式(,或者我必须指定多个路由提供程序条目,例如
$routeProvider.
when('/date', {
controller: 'myCtrl'
})
when('/name', {
controller: 'myCtrl'
})
.otherwise({
templateUrl: 'error.html'
});
如果这是唯一的解决方案,请跟进问题:
在我的控制器中,我使用 routeParams 检索 :sort 的值,这非常方便。
如果我不使用命名组,而是为每个有效的排序值提供一个$routeProvider条目,则排序值将不会包含在$routeParams中。
然后我是否必须解析路径才能获得正确的排序值,或者是否有更直接的方法?
谢谢。
$routeChangeStart就是你要找的!
只需查看$route的文档,每当路由更改时,都会发生此事件。
$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 是你的正则表达式函数