我正在尝试限制路由,以便只有 '/route1'
或 '/route2'
解析为以下规则:
$stateProvider.state("promotional", {
url: "/{contentType}",
templateUrl: coreConfig.path() + "/modules/content/content.tmpl.html",
controller: "promotionalController",
controllerAs: "contentCtrl",
data: { requiresLogin: true }
});
对于url
我已经尝试过
url: "/{contentType: (route1|route2)}"
url: "/{contentType: [(route1|route2)]}"
url: "/{contentType: [route1|route2]}"
这些似乎都不起作用。有什么解决办法吗?
最佳答案
我们可以像这样使用正则表达式:
// just routes 'route1' or 'route2'
$stateProvider
.state("promotional", {
url: "/{contentType:(?:route1|route2)}",
templateUrl: coreConfig.path() + "/modules/content/content.tmpl.html",
controller: "promotionalController",
controllerAs: "contentCtrl",
data: { requiresLogin: true }
})
// similar but only for 'route3' or 'route4' or 'route5'
.state("other", {
url: "/{contentType:(?:route3|route4|route5)}",
templateUrl: coreConfig.path() + "/modules/content/content.tmpl.html",
controller: "promotionalController",
controllerAs: "contentCtrl",
data: { requiresLogin: true }
})
类似问题:
- Matching url with array list of words in AngularJS ui-router
- AngularJS ui-router - two identical route groups
检查上面的例子here
关于regex - UI-Router 的简单正则表达式路由约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30445836/