Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event each time component becomes visible

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.

like image 945
Shimrod Avatar asked Nov 17 '16 09:11

Shimrod


People also ask

Is angular component visible?

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.


1 Answers

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;     } } 
like image 175
Shimrod Avatar answered Sep 19 '22 08:09

Shimrod