如何知道angular ui-router自定义数据是否从父状态继承 [英] how to know whether angular ui-router custom data is inherited from the parent state

查看:16
本文介绍了如何知道angular ui-router自定义数据是否从父状态继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一个状态中一个状态的数据字段是否继承自其父级.例如,

I want to find out whether the data field of a state in a state is inherited from its parent or not. For example,

    $stateProvider.state('parent', {
      data:{
         customData1:  "Hello",
      }
   })
   .state('parent.child', {
      data:{

      }
   });

这里,子状态(parent.child)没有定义自定义数据.它从其父(父)继承了 CustomData1.我想知道 parent.child 中的数据是否被继承.但是我不想做类似

Here, the child state(parent.child) has no custom data defined.It inherits CustomData1 from its parent(parent). I want to find out whether the data in parent.child is inherited or not.However I dont want to do something like

if(parent.child.data.customData1==parent.data.customData1)
  {
  }

有没有其他方法可以找到这个?

Is there any other way of finding this out?

推荐答案

我不确定,你的问题到底是什么...我已经尝试在这里解释它是如何工作的:

I am not sure, what exactly is your problem here... and I already tried to explain how it works here:

我创建了一个 plunker 展示了它如何为您服务.

I created a plunker showing how it could work for you.

让我们拥有这些状态.首先是一些父/子 - 子覆盖所有

Let's have these states. Firstly some parent / child - where child overrides that all

.state('parent', {
  url: '/parent',
  templateUrl: 'tpl.html',
  controller: 'controllerParent',
  data:{
     customData1:  "Hello",
     customData2:  "World!"
  }
})
.state('parent.child', {
  url: '/child',
  templateUrl: 'tpl.child.html',
  controller: 'controllerChild',
  data:{
     customData1:  "Goodbye",
     customData2:  "Problems"
  }
});

还有其他一些父子.customData1 此处不变

And also some other parent child. The customData1 is unchanged here

// Start 
.state('other', {
  url: "/other",
  templateUrl: 'tpl.html',
  controller: 'controllerParent',
  data:{
     customData1:  "Hello",
     customData2:  "Other World!"
  }
})
.state('other.child', {
  url: "/child",
  templateUrl: 'tpl.child.html',
  controller: 'controllerChild',
  data:{
     customData2:  "UI-Router!"
  }
})

所以我们可以看到,paren.child 根本不匹配自定义数据,而其他至少匹配 customData1.所以,如果父子数据键匹配,这个检查总是会给我们答案:

So we can see, that paren.child do not match custom data at all, whil other matches at least the customData1. So, this check will always give us answer, if the parent and child data key is matching:

  $scope.isSame = function(dataKey){
     var childData = $state.current.data;  
     var parentData = $state.$current.parent.data;  
     return childData[dataKey] === parentData[dataKey];
  };

plunker 像这样使用它:

And the plunker uses it like:

 {{isSame('customData2')}}

这里

这篇关于如何知道angular ui-router自定义数据是否从父状态继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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