我注意到当我在ngIf *下有一些元素时,即使在满足条件的情况下,在ngOnInit()上这些元素也不存在。呈现在ngIf *下的元素时,我该如何“捕捉”(我需要在其上调用一些函数)
答案 0 :(得分:0)
使用ngAfterViewInit()
ngAfterViewInit()。它是一个生命周期挂钩,在组件的视图已完全初始化之后被调用。
export class AfterViewInitDemoComponent implements AfterViewInit {
ngAfterViewInit() {
console.log("---ngAfterViewInit() Demo---");
}
}
请检查以下链接以获取更多信息
https://angular.io/api/core/AfterViewInit
https://www.concretepage.com/angular/angular-ngafterviewinit
答案 1 :(得分:0)
如果使用@ViewChild,请尝试使用static: true
参数。
@ViewChild('myElem', { static: true }) myElem: MyElementType;