Angularjs ui router,路由嵌套 父controller執行問題

來源:互聯網
上載者:User

標籤: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執行問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.