2015-11-02 141 views
16

獲取上下文,角度,ui路由器,沒什麼特別的,用3個命名的ui-views構建的根視圖。 所以在index.html我們

<body> 
    <div ui-view='left'> 
    <div ui-view='center'> 
    <div ui-view='right'> 
</body> 

我的路線看起來像

$stateProvider 
.state('main', { 
    url: '/', 
    views: { 
    'left': {templateUrl: 'foo.html'}, 
    'center': {templateUrl: 'bar.html'}, 
    'right': {templateUrl: 'xyz.html'} 
    } 
}) 
.state('main.b', { 
    url: '/b', 
    params: { foo: {value: 'bar'} } 
    views: { '[email protected]': {templateUrl: '123.html'} } // I wish to update $stateParams in '[email protected]' view 
}) 
.state('main.c', { 
    url: '/c', 
    params: ... 
    views: { '[email protected]': ..., '[email protected]': ..., '[email protected]': .. } 
}); 

是否有去B態在「中心」和「左」的視圖更新$ stateParams的方式?我可以使用服務來獲得它,但是我需要爲我需要的變量添加一個$watch,它對我來說看起來有點冒險。

進入c狀態我實際上可以得到我想要的,但視圖被重新加載,並且我希望避免這種行爲,因爲我在'左'視圖中有一個畫布。

回答

23

你可以使用下面的方法去一個特定的路由,而不需要重新加載的觀點:

$state.go('.', {parm1: 1}, {notify: false}); 

的最後一個對象常量表示,你可以沿着傳遞給go的選項。如果將notify設置爲false,這實際上會阻止控制器重新初始化。開頭的.是您想要轉到的絕對狀態名稱或相對狀態路徑。

雖然重要的是notify

+3

這在某種程度上還是引起視圖中的所有組件$初始化,然後立即$破壞,這可能會導致一些嚴重的性能的問題。你可以看到你的組件中是否添加了'console.log()'這個'.onnit()'和'this.onDestroy()'。 –

7

報價從https://github.com/angular-ui/ui-router/issues/1758#issuecomment-205060258 @christopherthielen:使用

通知:假是幾乎從來沒有一個好主意,現在 棄用。如果您必須使用reloadOnSearch。

您也可以嘗試1.0版本中的動態參數(當前爲 1.0.0-alpha.3)。在你的狀態,配置參數動態和落實uiOnParamsChanged回調:

.state('foo', { 
    url: '/:fooId', 
    params: { fooId: { dynamic: true } }, 
    controller: function() { 
    this.uiOnParamsChanged = function(changedParams, $transition$) { 
     // do something with the changed params 
     // you can inspect $transition$ to see the what triggered the dynamic params change. 
    } 
    } 
}); 

對於一個演示,看看這個plunker:http://plnkr.co/edit/T2scUAq0ljnZhPqkIshB?p=preview

+0

引起我不安的是,如果你在stateProvider定義中定義了'views',那麼在'views'定義中定義'controller'屬性,而不是在外面。例如,在文檔:https://ui-router.github.io/docs/latest/interfaces/ng1.ng1statedeclaration.html#views '的觀點:{ 頭:{ 控制器:函數(){ this.uiOnParamsChanged =功能(changedParams,$過渡$){ ... }} , templateUrl: 「header.html中」 }, 正文:{ 控制器: 「bodyCtrl」, templateUrl: 「body.html」 } , 頁腳:「footerComponent」 }' – ako977

6

我覺得用「動態PARAMS」現在是更好的解決方案:

當dynamic爲true時,對參數值的更改不會導致進入/退出狀態。解決方案不會被重新獲取,也不會重新加載視圖。

$stateProvider.state('search', { 
    url: '/search?city&startDate&endDate', 
    templateUrl: 'some/url/template.html', 
    params: { 
     city: { 
     value: 'Boston', 
     dynamic: true 
     } 
    } 
} 

然後:

$state.go('.', {city: 'London'}); 

https://ui-router.github.io/ng1/docs/latest/interfaces/params.paramdeclaration.html#dynamic https://github.com/angular-ui/ui-router/issues/2709

+0

感謝您的更新答案,但問題是我們現在如何區分$ stat導致的路線更改e.go從你的例子(動態參數)和有人剛回來使用瀏覽器後退按鈕? – pootzko