2015-05-17 91 views
7
改变

网址我有一个参数定义路线:

$stateProvider 
.state('item', {..}) 
.state('item.view', { 
    url: 'item/:id' 
    template: '...', 
    controller: '...' 
}); 

虽然已经在item.view状态,我想与切换网址

$state.go('item.view', {id: 'someId'}) 

无需重新加载页面。我曾尝试过:

$state.go('item.view', {id: 'someId'}, {notify: false}); 
$state.go('item.view', {id: 'someId'}, {notify: false, reload: false}); 

两者仍然会导致页面重新加载。我想我可能会运行到这里描述的问题:https://github.com/angular-ui/ui-router/issues/1758

+3

看看这个SO帖子:[link](http://stackoverflow.com/questions/23585065/angularjs-ui-router-change-url-without-reloading-state)看来你可以使用'$ state.transitionTo'(而不是'$ state.go')并设置可选参数以避免重载。在该链接中的一个评论(具有最高数量的选票)提示如下:'$ state.transitionTo('。detail',{id:newId},{location:true,inherit:true,relative:$ state。 $ current,notify:false})所以基本上设置通知为false和位置为真 - 评论时间70年01月01日原作者:Arjen de Vries –

回答

2

这应该是很容易:

// It needs to be executed on main scope, hence $timeout 
// If you are calling from a controller this is a must 
// Otherwise it will 'lag' and update on next function call 
$timeout(function() { 
    // As Sheryar Abbasi commented, the rest is simple. 
    // You should call $state.transitionTo with notify: false 
    // to stop the reload. 
    $state.transitionTo(
     'item.view', 
     { 
      id: 4378 // New id/$stateParams go here 
     }, 
     { 
      location: true, // This makes it update URL 
      inherit: true, 
      relative: $state.$current, 
      notify: false // This makes it not reload 
     } 
    ); 
}); 

This StackOverflow thread帮我找出$超时。

Here是官方的ui路由器快速参考。

相关问题