问题描述
我有一个带有解析承诺的抽象父状态,它返回有关当前用户的信息.这些信息被注入到所有子状态的控制器中.
I have an abstract parent state with a resolve promise which returns information about the current user. This information is injected into controllers of all the child states.
但是,当我使用 $state.go('parent.child')
时,未执行解析承诺函数.但是,如果我浏览到表示该状态的 URL,它会正常执行.
However when I use $state.go('parent.child')
the resolve promise function is not being executed. If I browse to the URL representing that state though, it executes fine.
我是否需要在每个子状态上指定解析对象并从父状态中省略它?
Do I need to specify the resolve object on each child state and omit it from the parent?
推荐答案
对于给定的 $stateParams
值集,父状态解析只会解析一次.如果父状态不依赖于 $stateParams
或者不使用任何,那么它的依赖只会被解析一次,不管子状态发生任何变化.
A parent state resolve will only resolve once for a given set of $stateParams
values. If the parent state does not rely on $stateParams
or does not use any, then its dependencies will only be resolved once, regardless of any child state changes.
您看到的行为差异是子状态更改不会导致父资源重新加载,而位置更改会随着完整的父子状态重新加载.
The difference in behaviour you are seeing is a child state change will not result in the parent resources being reloaded, whereas the location change will as the full parent and child states are reloaded.
您可以在这个 plunk 中观察这种行为.
You can observe this behaviour in this plunk.
该示例在子项所依赖的父状态中有一个 $stateParams
值.通过 $state.go
或 ui-sref
(两种方法都提供)更改状态将导致父资源的刷新.但是,将状态更改为子状态而不更改 $stateParams
参数将不会刷新父资源,并且将提供之前返回的值.
The example has a $stateParams
value in the parent state on which the children are dependent. Changing state via $state.go
or ui-sref
(both methods are provided) will result in a refresh of the parent resource. However, changing state to a child state without a change to the $stateParams
parameter will not refresh the parent resource and the previously returned value will be provided.
这篇关于ui-router 在使用 $state.go() 时不运行父状态的解析承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!