本文介绍了组件可见时发生的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的组件可见时,Angular2中是否有办法触发事件?它放在一个tabcontrol中,当用户切换我希望我的组件触发事件时,我想得到通知.

Is there a way in Angular2 to have an event fired when my component becomes visible ?It is placed in a tabcontrol and I want to be notified when the user switches I'd like my component to fire an event.

推荐答案

我最后做的(虽然不是很漂亮,但是在我没有更好的方法时可以工作...)是使用ngAfterContentChecked()回调并自己处理更改.

What I finally did (which is not very beautiful but works while I don't have a better way to do it...) is to use the ngAfterContentChecked() callback and handle the change myself.

@ViewChild('map') m;
private isVisible: boolean = false;
ngAfterContentChecked(): void
{
    if (this.isVisible == false && this.m.nativeElement.offsetParent != null)
    {
        console.log('isVisible switched from false to true');
        this.isVisible = true;
        this.Refresh();
    }
    else if (this.isVisible == true && this.m.nativeElement.offsetParent == null)
    {
        console.log('isVisible switched from true to false');
        this.isVisible = false;
    }
}

这篇关于组件可见时发生的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:32