0

I notice that when I have some elements under ngIf*, on ngOnInit() those elements don't exists even when the condition is met. How can I "catch" the moment when an element that is under ngIf* is rendered (I need to call some function on it)

user3362334
  • 1,980
  • 3
  • 26
  • 58

2 Answers2

0

use ngAfterViewInit()

ngAfterViewInit() is called after a component's view, and its children's views, are created. Its a lifecycle hook that is called after a component's view has been fully initialized.

export class AfterViewInitDemoComponent implements AfterViewInit { 
  ngAfterViewInit() {
    console.log("---ngAfterViewInit() Demo---");
  }
} 

please check following links for more info

https://angular.io/api/core/AfterViewInit

https://www.concretepage.com/angular/angular-ngafterviewinit

Shlok Nangia
  • 2,355
  • 3
  • 16
  • 24
0

If you use @ViewChild, try to use static: true parameter.

@ViewChild('myElem', { static: true }) myElem: MyElementType;

Muhammet Can TONBUL
  • 3,217
  • 3
  • 25
  • 37