【问题标题】:AngularJS Route : set several parameters values for the same routeAngularJS Route:为同一路由设置多个参数值
【发布时间】: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


【解决方案1】:

你不能做你想做的事。如果您想使用 ui-router,您可以将其关闭,但 AngularJS ngRoute 无法让您使用正则表达式或将路由参数绑定到类型。如果您愿意切换到 ui-router,我可以给您一个示例,说明如何做您想做的事情。

【讨论】:

    【解决方案2】:

    如果它可以帮助我实现我想做的事情,我愿意切换到 ui-router。 此外,如果它支持类型,那很好,因为我有必须是整数的参数。我将看一些例子。

    如果你有任何正则表达式的例子,那就太好了!

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 2021-02-24
      • 2017-09-11
      • 2018-01-04
      • 2013-05-26
      相关资源
      最近更新 更多