角 - 手表ngShow / ngHide变化祖先是影响儿童DOM元素的可见性 [英] Angular - Watch for ngShow/ngHide Changes in Ancestors that Affect Child DOM Element's Visibility

查看:99
本文介绍了角 - 手表ngShow / ngHide变化祖先是影响儿童DOM元素的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个DOM元素(绑定到,也就是说,一个表)。我是一个基于ngShow的选项卡控制的下游子。我住在第二个选项卡。当呈现页面时,我的存在,但我没有真正看到呢。

I am a DOM element (bound to, say, a table). I am the downstream child of an ngShow-based tabs control. I live on the second tab. When the page is rendered, I exist but I am not really visible yet.

我想以某种方式得到通知时,我通过我的不是,直属母公司,可见当其ngShow前pression为真。

I would like to somehow be notified when I become visible through my not-immediate parent, when its ngShow expression becomes true.

这可能吗?我想避免增加code以外的任何地方我自己 - 例如,我宁愿不以Tab改变事件添加code的地方。我宁愿有办法注册,找出时,需要通过通过ngShow / ngHide,我的知名度变化必将在最外层容器的变化。可以这样做?

Is this possible? I would like to avoid adding code anywhere outside myself - for example, I'd rather not add code in a tab-changed event somewhere. I'd rather have a way to register to find out when, via changes in outermost containers that are bound via ngShow/ngHide, my visibility changes. Can this be done?

推荐答案

既然你已经在指令中可用的范围,则可以使用它来监视能见度的变化:

Since you have a scope available in the directive, you could use this to watch for changes in visibility:

scope.$watch(function() { return element.is(':visible') }, function() {
  // Do whatever should happen when the visibility changes
});

这避免了轮询的知名度,这可能是一个性能命中。当然,这是假定的范围是由任何导致可视性改变施加。我试着在我自己的指令,但没有涉及到ngShow / ngHide。

This avoids polling the visibility, which could be a performance hit. Of course, this assumes that the scope is applied by whatever causes the visibility to change. I've tried it in my own directive, but not related to ngShow/ngHide.

信贷,信贷是由于:这个最初提出的(不是我)在这里:

<一href=\"https://groups.google.com/d/msg/angular/w7Gxa5M_17M/koYCZnsobawJ\">https://groups.google.com/d/msg/angular/w7Gxa5M_17M/koYCZnsobawJ

Credit where credit is due: this was originally proposed (not by me) here:
https://groups.google.com/d/msg/angular/w7Gxa5M_17M/koYCZnsobawJ

修改2014年2月24日

重温这个答案几个月laters,我注意到这工作,但会相当慢一点的消化周期。最佳做法是不使用昂贵的功能,例如本作脏检查。请谨慎使用!

EDIT 2014-02-24
Revisiting this answer a few months laters, I have noticed that this works but will slow down your digest cycle quite a bit. Best practice is to not use expensive functions such as this for dirty checking. Use with caution!

这篇关于角 - 手表ngShow / ngHide变化祖先是影响儿童DOM元素的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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