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

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

问题描述

有没有办法从父状态获取参数.

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

我的状态是这样的.

    $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 路由器.见:

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

小引用:

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

Child states DO inherit the following from parent states:

  • 通过resolve解决依赖
  • 自定义数据{}属性
  • 没有任何被继承(没有控制器、模板、网址等).

    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天全站免登陆