如何在angular.js中实现自定义路由?

Jak*_*kub 1 state angularjs angular-ui-router

我有一条路线:

.state('home.blog', {
  url: "/blog",
  templateUrl: "blog.html",
  controller: "BlogController"
})
Run Code Online (Sandbox Code Playgroud)

我的路线是localhost:3000/home/blog我想改变它localhost:3000/blog.我搜索了互联网,但我没有找到任何简单的解决方案.

Rad*_*ler 5

这是因为url部分是从父级中删除的.但是我们可以明确设置,' ^ ' 不应该使用父级:

.state('home.blog', {
  url: "^/blog",
  templateUrl: "blog.html",
  controller: "BlogController"
})
Run Code Online (Sandbox Code Playgroud)

见文档

绝对路线(^)

如果您想要绝对URL匹配,那么您需要在url字符串前面加上特殊符号'^'.

$stateProvider
  .state('contacts', {
     url: '/contacts',
     ...
  })
  .state('contacts.list', {
     url: '^/list',
     ...
  });
Run Code Online (Sandbox Code Playgroud)