ui-router Go创建双路径元素

时间:2015-09-01 07:49:05

标签: javascript angularjs routing angular-ui-router angularjs-routing

我在角度Meteor项目中使用UI-Router。 当我在路线/搜索并想要重新定位到/ search /:term(例如/ search / starwars)并且我在我的控制器中使用

$state.go( 'private.search.feed', { term: 'starwars' } )

然后我的浏览器中的网址会转到/search/search/starwars而不是/search/starwars

我的路线设置(我有2个布局,因此是抽象视图)

$stateProvider

  # abstract views / layout
  .state 'private',
    url: ''
    abstract: true
    views:
      'app':
        templateUrl: 'client/views/layouts/privateLayout.html'

  .state 'public',
    url: ''
    abstract: true
    views:
      'app':
        templateUrl: 'client/views/layouts/publicLayout.html'

  # end abstract views / layout

  .state 'private.home',
    url: '/'
    views:
      "container@private":
        templateUrl: 'client/views/home/home.html'


  .state 'private.search',
    url: '/search'
    views:
      "container@private":
        templateUrl:  'client/views/search/search.html'
        controller:   'searchCtrl'

  .state 'private.search.feed',
    url: '/search/:term'
    views:
      "container@private":
        templateUrl:  'client/views/search/feed/feed.html'
        controller:   'searchCtrl'

当我将路线更改为

.state 'private.searchFeed',
    url: '/search/:term'
    views:
      "container@private":
        templateUrl:  'client/views/search/feed/feed.html'
        controller:   'searchCtrl'

并致电

$state.go( 'private.searchFeed', { term: 'starwars' } )

网址是正确的。

我估计我在第一次设置中定义了一个子状态,但我不明白为什么它会在URL中重复search

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

private.search.feed状态网址应为'/:term',因为父状态的网址是从其派生的状态继承的。由于您的派生状态必须类似于/:term,因此显然会通过/search/:term

变为/可访问

<强>代码

.state 'private.search.feed',
    url: '/:term'
    views:
      "container@private":
        templateUrl:  'client/views/search/feed/feed.html'
        controller:   'searchCtrl'

答案 1 :(得分:1)

在状态名称中使用 dot 表示ui-router它是一个子状态 dotpath定义的任何状态。例如:

State 1: home (/home)
State 2: home.dashboard (/home/dashboard)
State 3: home.dashboard.settings (/home/dashboard/settings)

您无法创建名为home.dashboard.settings.save的新状态状态4 ,其路由不以状态3 的URL开头,因为状态4 dotpath 表示它应该继承自 State 3

因此,您需要确保子状态的网址不会重复其父网址:

.state 'private.search.feed',
    url: '/:term'