简体   繁体   English

Angular - 调用 ngOnInit() 时,ngIf* 下的元素不会呈现

[英]Angular - Elements that are under ngIf* doesn't render when ngOnInit() is called

I notice that when I have some elements under ngIf*, on ngOnInit() those elements don't exists even when the condition is met.我注意到当我在 ngIf* 下有一些元素时,在 ngOnInit() 上,即使满足条件,这些元素也不存在。 How can I "catch" the moment when an element that is under ngIf* is rendered (I need to call some function on it)如何“捕捉”呈现 ngIf* 下的元素的时刻(我需要在其上调用一些 function)

use ngAfterViewInit()使用ngAfterViewInit()

ngAfterViewInit() is called after a component's view, and its children's views, are created. ngAfterViewInit() 在组件视图及其子视图创建后调用。 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://angular.io/api/core/AfterViewInit

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

If you use @ViewChild, try to use static: true parameter.如果您使用@ViewChild,请尝试使用static: true参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM