【问题标题】:how to implement custom routes in angular.js?如何在 angular.js 中实现自定义路由?
【发布时间】:2015-07-17 17:26:52
【问题描述】:

我有一条路线:

.state('home.blog', {
  url: "/blog",
  templateUrl: "blog.html",
  controller: "BlogController"
})

我的路线是localhost:3000/home/blog,我想把它改成localhost:3000/blog。我在网上搜索了但没有找到任何简单的解决方案。

【问题讨论】:

    标签: angularjs angular-ui-router state


    【解决方案1】:

    这是因为 url 部分来自父级。但我们可以显式设置,'^' 不应使用该父级:

    .state('home.blog', {
      url: "^/blog",
      templateUrl: "blog.html",
      controller: "BlogController"
    })
    

    查看文档

    Absolute Routes (^)

    如果你想进行绝对 url 匹配,那么你需要在你的 url 字符串前面加上一个特殊符号 '^'。

    $stateProvider
      .state('contacts', {
         url: '/contacts',
         ...
      })
      .state('contacts.list', {
         url: '^/list',
         ...
      });
    

    【讨论】:

    • 太棒了!非常感谢!
    • 很高兴看到 Jakub!享受很棒的 UI-Router ;)
    猜你喜欢
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多