in ui-router, how to re-resolve only the most local state?

蓝咒 提交于 2019-12-12 03:34:25

问题


I have the following state tree, using ui-router,

1 login
2 root (abstract, resolves app-prefs, user-prefs)
    2.1 home (builds a refresh button, should refresh whatever is being shown)
        2.1.1 dashboard (resolves dashboard-prefs)
        2.1.2 search (resolves search-prefs)
        2.1.3 etc
    2.2 etc

From home when user presses refresh button while in XYZ state, I would like to have the XYZ re-entered in such a way that it re-resolves its own XYZ-prefs but not things above in hierarchy. Something like

$state.go("dashboard", dashboardParams, {please-resolve-only-dashboard})

When I try, from home

$state.go("dashboard", dashboardParams, {reload:true})

that causes everything from root downwards to get re-resolved, which is problematic, and expensive, as I need to re-resolve only dashboard-prefs. I can setup a more elaborate scheme in some resolvers to not re-resolve themselves but that might become a task by itself I'm afraid. Is there another, more idiomatic way?

Thanks


回答1:


There is a wokring plunker

There is a native way how to do that - just change the parameter of the state you want to reload.

To reproduce the above state definition let's have dashboard defined like this:

.state('dashboard', { 
  parnet: 'home',
  url: "^/dashboard",
  params: { updater : 1, },
  ...
})

What we can see, that we do not touch url at all. It will always be without any change just /dashboard

But we introduce really cool feature of the latest version - params: {}. It defines some parameter - updater in our case. Whenever this parameter is sent, and does differ form its current value, this child state (and only this child state) is re-init

Check it more about state params: {} here: http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateProvider

Now, we can create this reload link:

<a ui-sref="dashboard({updater : ($stateParams.updater + 1) })">reload</a>

And with this incrementation, we can be sure, reload will reload this state

Check it here



来源:https://stackoverflow.com/questions/29640338/in-ui-router-how-to-re-resolve-only-the-most-local-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!