0

I want to call method when component is visible. Problem is that component is not visible but hook ngAfterViewInit is called anyway. What can I do to call it after component is visible.

ngAfterViewInit(): void {
 methodToBeCalled();
}
Juraj Jakubov
  • 291
  • 4
  • 13
  • What do you mean by "visible"? Also, what is the content of your template? – JSON Derulo May 17 '23 at 13:55
  • Please expand the question a bit more, what do you mean by "visible"? – Johalternate May 17 '23 at 13:59
  • I cannot reproduce it... Have a look at this [stackblitz](https://stackblitz.com/edit/angular-bgaabr). On after view init, the element is definitely rendered, as you can see in the console output. Please add a minimal, reproducible code example and a specific expected behavior. "It should be visible" is not enough. – JSON Derulo May 17 '23 at 14:07
  • @JSONDerulo well it seems then like some internal problem. Basically in methodToBeCalled() i am setting value to HTML element. But this element is null because its not yet rendered/visible like everything else. So its interresting. – Juraj Jakubov May 17 '23 at 14:32
  • 1
    It can happen if you conditionally render the element using `*ngIf`, but then it would be expected behavior... – JSON Derulo May 17 '23 at 14:35
  • This component itself is inserted into component that is rendered using *ngIf. Anyway is there any way how to overcome this? – Juraj Jakubov May 17 '23 at 14:39
  • I tried using detectChanges the old way and it seems to work instead of this way so we can say is closed anywas if u know way how to overcome that problem please feel free to post it as answer – Juraj Jakubov May 17 '23 at 14:44

0 Answers0