Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular ui-router and accessing child state params from parent

Edited with more details

Newbie question here, I am sure. I am trying to do something that I thought would be straightforward, but I am not getting anywhere.

Basically, I have a parent state with several child states. I want to load some data in the resolve of the parent state so that it is available to all child states. To do this, I need a parameter (context) that is passed to the child state to be available. I am getting it via $state.params. the code (some omitted for clarity) is like this:

.state('pbr', {
    url: "/pbr",
    templateUrl: "pbr/pbr.html",
    controller: "pbrBaseCtrl",
    abstract: true ,
    resolve: {
        dpts: function(NavDPT, $state) {
               return NavDPT.query({context: $state.params.context}).$promise;
              }
        }    

})
.state('pbr.review', {
        url: "/review/{context}?div",
        templateUrl: "pbr/pbr-review.html",
        controller: "pbrReviewCtrl"
    })

From what I understand, $state.params should contain the passed parameters for the parent and child states whereas $stateParams have the params only for the active state.

Anyway, what I am seeing is that $state.params does not get populated before the resolve happens, and thus the fetch of data fails. It does eventually get populated though. I can't seem to work out a way to wait for $state.params to be available before getting the data. I have tried wrapping the thing in a timeout, etc.

I have tried to instead use query params, and have specified the query param at the parent level, and I observe this same behavior - newcontext is not available yet when the resolve happens.

 .state('pbr', {
            url: "/pbr?newcontext",
            templateUrl: "pbr/pbr.html",
            controller: "pbrBaseCtrl",
            abstract: true ,
            resolve: {
                dptsResource: 'NavDPT',
                dpts: function(dptsResource, $state, $log) {
                    $log.info("state.params is also ",$state.params);
                    return dptsResource.query({context: $state.params.newcontext}).$promise;
                }
            }

        })

Thanks.

like image 548
Chris Perreault Avatar asked Jun 19 '14 08:06

Chris Perreault


1 Answers

There is an example showing how we can acces the $stateParams even in resolve function, but they must be defined in current or parent state (not in child)

So, these are the states:

  $stateProvider
    .state('pbr', {
        // we define 2 params oldContext and newContext
        url: "/pbr/{oldContext}?newContext",
        ...
        resolve: { 
            // here we can get all params defined for our state
            dpts: function($state, $stateParams) { 
                return {
                  oldContext : $stateParams.oldContext,
                  newContext : $stateParams.newContext,
                }
            }
        }
    })
    // child will have even the context as 3rd param
    .state('pbr.review', {
      url: "/review/{context}",
      ...
    })

These are the ways how to call them:

// via ui-sref
ui-sref="pbr.review({oldContext: 'abc1', newContext : 'def1', context : 'xyz1'})"
ui-sref="pbr.review({oldContext: 'abc2', newContext : 'def2', context : 'xyz2'})"

// via href
href="#/pbr/abc1/review/xyz1?newContext=def1"
href="#/pbr/abc2/review/xyz2?newContext=def2"

Observe more in that plunker

like image 130
Radim Köhler Avatar answered Oct 21 '22 17:10

Radim Köhler