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.
Angular doesn't have a way to know when a component becomes visible if it's just hidden behind another DOM element.. If you use *ngIf="" to show/hide, then component when the tab is selected or deselected, then ngAfterViewInit() is called every time.
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With