UI-Router: sequential resolve of parent and child states

All we need is an easy explanation of the problem, so here it is.

I have two abstract states parent and parent.child, and an activateable state parent.child.grand.

I want parent to be promise resolved before parent.child.grand gets its resolves executed. Why? Because a certain data which comes from the ajax request in the resolve from parent is required inparent.grand.child.

Here’s a gist

Is it possible to sequentially chain the promises of parent to children states without using controllers ?

(parent resolve start -> finish ajax request -> resolve promise -> parent.child.grand resolve start -> finish ajax request -> resolve promise)

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I’ve seen a few answers for this but it still wasn’t entirely clear without an example. The docs say:

The resolve keys MUST be injected into the child states if you want to
wait for the promises to be resolved before instantiating the
children.

Here’s the example:

    $stateProvider.state('parent', {
          resolve:{
             resA:  function(){
                return {'value': 'A'};
             }
          },
          controller: 'parentCtrl'
       })
       .state('parent.child', {
          resolve:{
             // Adding resA as an argument here makes it so that this child state's resB resolve
             // function is not run until the parent state's resA resolve function is completed.
             resB: function(resA){
                return {'value': resA.value + 'B'};
             }
          }
          controller: 'childCtrl'
        })

And you don’t have to inject resA into the child controller.

Method 2

If you add auth to the dependency injection of your grandchild resolve, ui-router will resolve it prior to the grandchild resolve.

['authsrv', 'auth', function(authsrv, auth){}]

Hope that makes sense.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply