stateChangeStart中子状态的父状态的访问参数 [英] Acess parameters of parent state from child state in stateChangeStart

查看:106
本文介绍了stateChangeStart中子状态的父状态的访问参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以从父状态获取参数.

Is there any way to get the parameters from parent state.

我的状态看起来像这样.

My states look like this.

    $stateProvider
        .state('home', {
            url: '/home/:param1',
            template: '<div>home<ui-view /></div>',
            data: {
            parentdata: 'parentdata'
                }
          })
        .state('home.child', {
            url: '/child/:param2',
            template: '<div>index</div>',
            data: {
            childdata: 'childdata'
                }
          })
})

我想从子状态访问父状态的数据值.

I want to access the data value of parent state from child state.

 $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {    
     var tosateParam = toState.data.mycustomparam;     
     //If this is the child state, how can I access the parent state param     
      //may be like this toState.parent.data.parentParam


   });

如果我当前的状态名称是"ABC.childstate",如何从ABC状态访问参数.

If my current state name is "ABC.childstate", how can I access the parameters from ABC state.

推荐答案

如果我们需要访问父级data {},则必须...从UI-Router获取利润.参见:

In case we need to access parent data {}, we have to... profit from UI-Router. See:

小引用:

子状态 DO 从父状态继承以下内容:

Child states DO inherit the following from parent states:

  • 通过resolve解决的依赖项
  • 自定义 data {} 属性
  • 没有任何继承(没有控制器,模板,URL等).

    Nothing else is inherited (no controllers, templates, url, etc).

    有了此清晰的文档消息,我们可以具有类似这些的状态(请参见此处的工作示例):

    Having this clear doc message, we can have states like these (see working example here):

    $stateProvider
        .state('home', {
            url: '/home/:param1',
            data: { parentdata: 'parentdata', },
            ...
          })
        .state('home.child', {
            url: '/child/:param2',
            data: {  childdata: 'childdata', },
            ...
          })
    

    以及类似的链接:

    <a href="#/home/justAParent">
    <a href="#/home/first1/child/second1">
    <a href="#/home/first2/child/second2">
    

    将按照文档所述 结果(关于这样捕获的状态更改)

    $rootScope.$on('$stateChangeStart',
       function(event, toState  , toParams, fromState, fromParams) 
        {
          console.log(toState.data)
        });
    

    进入:

    // href #/home/justAParent will show
    Object {parentdata: "parentdata"}
    // both #/home/first1/child/second1
    //  and #/home/first2/child/second2 will show
    Object {parentdata: "parentdata", childdata: "childdata"}
    

    此处

    这篇关于stateChangeStart中子状态的父状态的访问参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆