【问题标题】:angular ui-router parent url set to /角度 ui-router 父 url 设置为 /
【发布时间】:2014-07-09 09:21:16
【问题描述】:

使用 ui-router 并将父状态 url 设置为 / 会导致我的 url 在查看子页面时看起来像 localhost:8000/#//child。我想在哈希和子节点之间只添加一个 /。

我尝试将父状态 url 留空,但使用锚链接时 ui-sref 不会链接回父状态。有没有人有一个解决方案来解决根级未命名的父 url?

app.config

app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");

$stateProvider
  .state("main", {
    url: "/",
    templateUrl: 'sites/main/main.html',
    controller : 'MainController',
    resolve: {
      items: function(srvMenu) {
          return srvMenu.getNav();
      }
  }

 })

});

child.config

emergency.config(function($stateProvider) { 
$stateProvider
    .state("main.emergency", {
      url: "/emergency",
      templateUrl: 'sites/emergency/emergency_menu.html',
      controller : 'EmenuController',
      resolve: {
          emenu: function(srvEmergency) {
              return srvEmergency.getMenu();
          }
      }

    })

    .state("main.emergency.detail", {
      url: "/detail",
      templateUrl: 'sites/emergency/emergency_detail.html',
    })
});

【问题讨论】:

  • 你能发布你的 app.config() 吗?我已经将 ui-router 与父级一起用作 / 并且从未见过这个问题...

标签: angularjs angular-ui-router


【解决方案1】:

ui-router 对任何事情都有解决方案(几乎)。有一个工作的plunker。我们将实际使用的功能称为:Absolute Routes (^)

所以,让我们从需求开始,拥有这些 URL 路由:

domain/#/                             -- root for main
domain/#/emergency/                   -- main.emergency
domain/#/emergency/detail             -- main.emergency.detail

在使用 ui-sref 状态调用时应该可以工作:

<ul>
    <li><a ui-sref="main">Main</a></li>
    <li><a ui-sref="main.emergency">Emergency</a></li>
    <li><a ui-sref="main.emergency.detail">Detail</a></li>
</ul> 

现在,诀窍是:

  • 根(first)状态必须使用url: "/",才能有一些唯一标识
  • 但下一个状态可以再次从根开始定义:使用此设置:
    url: "^/..."

这里是状​​态配置,启用想要的结果

  $stateProvider
    // root with '/'
    .state("main", {
      url: "/",
      templateUrl: 'main.tpl.html',
    })
    // here we start again from the root '/emergency'
    .state("main.emergency", {
      url: "^/emergency",
      templateUrl: 'emergency_menu.tpl.html',
    })
    // parent and child '/emergency/detail
    .state("main.emergency.detail", {
      url: "/detail",
      templateUrl: 'emergency_detail.tpl.html',
    });

这里是文档:

Absolute Routes (^)

...以及工作中的plunker...

【讨论】:

  • 正是我想要的!谢谢;)
  • 很高兴看到这一点,ui-router 是很棒的工具 ;)
猜你喜欢
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 2014-08-07
  • 2016-01-02
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 2015-10-14
相关资源
最近更新 更多