【发布时间】:2019-04-30 15:16:29
【问题描述】:
我想知道 AngularJS 是否可以做这样的事情:
我目前有一个路由,其第一个参数称为“资源”,可以是“设备”或“组”。还有一个名为“id”的参数,但这并不重要。使用以下代码,路由接受任何作为第一个参数:
.when("/templates/:resource/:id", {
templateUrl: "/templates/views/navigation/templates.html",
controller: 'ctrlTemplates',
tab: "templates",
})
如果第一个参数的值是“设备”或“组”,它会让我检查控制器。如果可能的话,我想摆脱这个验证部分而不创建两条路线:
.when("/templates/group/:id", {
templateUrl: "/templates/views/navigation/templates.html",
controller: 'ctrlTemplates',
tab: "templates",
})
.when("/templates/device/:id", {
templateUrl: "/templates/views/navigation/templates.html",
controller: 'ctrlTemplates',
tab: "templates",
})
所以我的问题是,是否有可能使用一条路线来拥有多个网址?例如这样的事情:
.when("/templates/('device'|'group')/:id", {
templateUrl: "/templates/views/navigation/templates.html",
controller: 'ctrlTemplates',
tab: "templates",
})
所以我以后不用自己在控制器中检查参数的值了
if([('group', 'device'].includes($routeParams.resource))...
你知道这是否可能吗?或者类似的方法?
最好的问候,
【问题讨论】:
标签: angularjs