4

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.

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?

n4cer500
  • 733
  • 7
  • 21

2 Answers2

9

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.

You can observe this behaviour in this plunk.

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.

do0g
  • 311
  • 2
  • 5
  • so what is the fix for this ? – rolling stone Mar 30 '15 at 10:01
  • I can't see how a fix is necessary. I'd surmise this behaviour to be by design as the parent resources have not changed. A full refresh can be achieved by navigating to the route using the `$location` service. – do0g Apr 15 '15 at 14:32
  • If you want to force reload you can do: $state.go('parent.child', {}, {reload: true}); – RKI Oct 01 '16 at 11:36
0

I think you just have a miss understanding of resolve, only the current state's resolves get called. You can just copy the resolve from the parent to the childs. I am pretty sure there is never a reason to even have a resolve on an abstract state. The abstract state will give you $stateParams, thats it though.

  • Thanks for your answer. If I have a resolve on the parent state, and visit the URL /child, the resolve function will be executed. There must be a difference between what runs in this case and what runs with $state.go – n4cer500 Aug 17 '14 at 22:57
  • 3
    @user3915433 `only the current state's resolves get called` Sorry, but this is not true. When navigating to a child state the parent state's dependencies will be resolved. The parent state's dependencies will also be re-resolved if any of its `$stateParams` change. See my answer for more information and an example. `I am pretty sure there is never a reason to even have a resolve on an abstract state.` There is; when one or more child states depend on the same resource. The parent state resolve means you do not have to resolve in the child states. – do0g Aug 18 '14 at 07:30