在 angular ui-router URL 中添加可选属性 [英] Prepend optional attribute in angular ui-router URL

查看:21
本文介绍了在 angular ui-router URL 中添加可选属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 ui-router 的 url 开头设置一个可选属性?如果是这样,如何?

Is there a way to have an optional attribute at the beginning of the url in ui-router? If so, how?

我想达到这样的目的:

.state('events',{
    url: '(/city:)?/events/:id'
    ...
})

所以城市"是可选的.

感谢您的任何意见.

推荐答案

让两个单独的路由指向同一个状态可能是不好的做法,但是没有什么可以阻止您创建使用相同分辨率/控制器/的两个不同状态模板.

It's probably bad practice to have two separate routes point to the same state, but nothing is keeping you from creating two different states that use the same resolutions/controller/template.

$stateProvider
.state('state1', {
  url: "/:city/events/:id",
  templateUrl: "partials/events.html",
  controller: eventsCtrl
})
.state('state2', {
  url: "/events/:id",
  templateUrl: "partials/events.html",
  controller: eventsCtrl
});

function eventsCtrl($scope){
  // shared controller
}

同样,这可能是不好的做法,但我想不出更好的解决方案

Again, this is probably bad practice but I can't think of a better solution

这篇关于在 angular ui-router URL 中添加可选属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆