angular ui-router and accessing child state params from parent

后端 未结 2 1791
情书的邮戳
情书的邮戳 2020-12-11 01:40

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

相关标签:
2条回答
  • 2020-12-11 02:20

    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

    0 讨论(0)
  • 2020-12-11 02:28

    Just define params on the parent level. You will then have access to them both on parent and child levels.

    PARENT:

    {
        state: 'modal',
        config: {
            url: '/modules?person_id&encounter_id',
            controller: 'ModalController',
            controllerAs: 'vm',
            templateUrl: 'app/modal/views/modal.html',
            abstract: true
        }
    }
    

    CHILD:

    {
        state: 'modal.micro',
        config: {
            url: '/micro',
            controller: 'MicroController',
            controllerAs: 'vm',
            templateUrl: 'app/micro/views/micro.html'
        }
    }
    

    ACCESS FROM MODAL CTRL (example):

    if (condition) {
        vm.currentPerson = _.find(DashboardModel.pane1Data.DATA_REC.DATA_LIST, function (person) {
            return person.PERSON_ID == $stateParams.person_id;
        });
    }
    

    ACCESS FROM CHILD CTRL (example):

    if (condition) {
        vm.currentPerson = _.find(DashboardModel.pane1Data.DATA_REC.DATA_LIST, function (person) {
            return person.PERSON_ID == $stateParams.person_id;
        });
    }
    
    0 讨论(0)
提交回复
热议问题