標籤:spec 原因 重複 iss tac like 重新整理 control commit
路由路徑設定:structured.text ;structured是第一層路由,text是第二層路由;
問題如下,當$state.transitionTo路由到第二層是,structured的controller也會執行一次,導致頁面控制項重新載入重新整理。
$state.transitionTo( "structured.text", { args : $stateParams }, { reload: false});
查了github看到 https://github.com/angular-ui/ui-router/issues/1612,說此問題已經被fixed,然而設定了reload: stateObj並沒有卵用。
christopherthielen commented on 8 Mar 2015
|
I think this makes sense and is a good idea. I‘d like to omit the second reloadState parameter, and overload the existing reload parameter.
{ reload: true }// reloads all,
{ reload: ‘foo.bar‘ } // reloads top.bar, and any children
{ reload: stateObj } // reloads state found in stateObj, and any children
Squash your commits and set your commit message to match the contributor guidelines. If @nateabeleconcurs, I think we‘ll merge this in. |
直到看到了https://stackoverflow.com/questions/21105528/difference-between-state-transitionto-and-state-go-in-angular-ui-router
$state.go(to [, toParams] [, options])
Returns a Promise representing the state of the transition.
Convenience method for transitioning to a new state. $state.go calls $state.transitionTointernally but automatically sets options to { location: true, inherit: true, relative: $state.$current, notify: true }. This allows you to easily use an absolute or relative to path and specify only the parameters you‘d like to update (while letting unspecified parameters inherit from the current state).
$state.transitionTo(to, toParams [, options])
Returns a Promise representing the state of the transition.
Low-level method for transitioning to a new state. $state.go() uses transitionTo internally. $state.go() is recommended in most situations.
$state.transitionTo是底層方法,用$state.go是用它實現的,$state.go設定了一些預設參數 ,於是用$state.go試了下,竟然成功;
debug看了下原來是預設options inherit:true和reload:false共同引發的問題:
錯誤寫法1:
$state.transitionTo( "structured.text", { args : $stateParams }, { reload: false});
原因:路由寫了兩級,structured的controller會被重複執行
錯誤寫法2:
$state.transitionTo( ".text", { args : $stateParams }, { reload: false});
原因:relative沒有設定和inherit預設又是false
正確寫法:
$state.transitionTo( ".text", { args : $stateParams }, { reload: false inherit:true, relative:"structured"});
或者
$state.go( ".text", { args : $stateParams }, { reload: false});
果然推薦使用$state.go, 用$state.transitionTo底層方法的話,options要都正確,問題解決,搞了半天事件,這api設計的讓人鬱悶。
Angularjs ui router,路由嵌套 父controller執行問題