角ui-路由器父url设置为/

时间:2022-06-16 19:40:17

Using ui-router and setting the parent state url to / results in my url looking like localhost:8000/#//child when looking at a child page. I'd like just a single / between the hash and child.

在查看子页面时,使用ui-router并将父状态url设置为/结果在我的url中,看起来像localhost:8000/#//子。我只想要一份哈希和孩子之间的。

I tried leaving the parent state url blank, but then ui-sref won't link back to the parent state when using an anchor link. Anybody got a solution for having a root level un-named parent url?

我尝试将父状态url留空,但是当使用锚点链接时ui-sref不会链接回父状态。有人找到了根级别未命名的父url的解决方案吗?

app.config

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

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',
    })
});

1 个解决方案

#1


24  

The ui-router has solution (almost) for anything. There is a working plunker. The feature we will se in action is called: Absolute Routes (^)

ui-router有任何解决方案(几乎)。有一个工作的活塞。这个功能我们将在行动被称为:se绝对路径(^)

So, let's start with requirement, to have these URL routes:

那么,让我们从需求开始,有这些URL路由:

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

Which should be working when using this ui-sref state calls:

在使用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> 

Now, the trick is:

现在,关键是:

  • the root (first) state must use url: "/", to have some unique identification
  • 根(第一个)状态必须使用url: "/",以具有某些惟一标识
  • but the next state can start its definition from the root again: with this setting:
    url: "^/..."
  • 但是下一个状态可以从根开始它的定义:这个设置:url:“^ /……”

Here is the state configuration, enabling the desired results

这里是状态配置,支持所需的结果

  $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',
    });

And here is the documentation:

这是文件

Absolute Routes (^)

...and the working plunker...

…和工作砰砰作响……

#1


24  

The ui-router has solution (almost) for anything. There is a working plunker. The feature we will se in action is called: Absolute Routes (^)

ui-router有任何解决方案(几乎)。有一个工作的活塞。这个功能我们将在行动被称为:se绝对路径(^)

So, let's start with requirement, to have these URL routes:

那么,让我们从需求开始,有这些URL路由:

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

Which should be working when using this ui-sref state calls:

在使用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> 

Now, the trick is:

现在,关键是:

  • the root (first) state must use url: "/", to have some unique identification
  • 根(第一个)状态必须使用url: "/",以具有某些惟一标识
  • but the next state can start its definition from the root again: with this setting:
    url: "^/..."
  • 但是下一个状态可以从根开始它的定义:这个设置:url:“^ /……”

Here is the state configuration, enabling the desired results

这里是状态配置,支持所需的结果

  $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',
    });

And here is the documentation:

这是文件

Absolute Routes (^)

...and the working plunker...

…和工作砰砰作响……