2015-05-17 86 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路由器快速參考。

相關問題