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

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

问题描述

我想找出是否在状态的状态的数据字段从其父母或不继承。例如,

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)没有自定义数据defined.It从它的父(母)继承CustomData1。我想找出parent.child的数据是否是继承还是not.However我不想做这样的事情。

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:

  • How to disable data inheritance of angular ui router states

我创建了一个 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')}}

检查一下这里

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

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