简体   繁体   English

使用带有 *ngIf 条件的 @viewchid 时获取未定义的 nativeElement

[英]Getting undefined nativeElement when using @viewchid with *ngIf condition

Getting undefined nativeElement when using @viewchid with *ngIf condition使用带有 *ngIf 条件的 @viewchid 时获取未定义的 nativeElement

Error错误

Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'nativeElement') Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'nativeElement')

@ViewChild('myElement', { static: false }) myElement: ElementRef<HTMLElement>;

不要在构造函数中调用它, ngAfterViewInit是第一个定义它的钩子。

If your `*ngIf condition is dynamic you should probably use a setter.如果您的 `*ngIf 条件是动态的,您可能应该使用 setter。

  @ViewChild('myElement') set setMyElement(myElement: ElementRef) {

    if (myElement) {
      // Here you can access content.nativeElement etc.
    }

  }

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

相关问题 使用@viewchild 时,无法以角度读取未定义的属性“nativeElement” - Cannot read property 'nativeElement' of undefined in angular when using @viewchild 在 angular 中将 *ngIf 与 else 一起使用时出现错误 - Getting error when using *ngIf with else in angular 如何解决使用viewchild ElementRef时无法读取角度未定义的属性&#39;nativeElement&#39; - How to solve Cannot read property 'nativeElement' of undefined in angular when using viewchild ElementRef 我发现 this.myScrollContainer.nativeElement.scrollHeight 但无法读取未定义的属性 &#39;nativeElement&#39; - i am finding this.myScrollContainer.nativeElement.scrollHeight but getting Cannot read property 'nativeElement' of undefined Ionic为什么nativeElement未定义? - Ionic why nativeElement of undefined? 为什么 elementRef 的 nativeElement 未定义? - Why is nativeElement of elementRef undefined? Angular 4 无法使用 ViewChild 读取未定义的属性 nativeElement - Angular 4 cannot read property nativeElement of undefined using ViewChild 为什么未定义nativeElement? - Why is nativeElement undefined? 无法使用 ngif 读取 null 的属性“nativeElement” - Cannot read property 'nativeElement' of null with ngif 在 Angular 的 ngIf 条件中使用 split 函数 - Using split function inside ngIf condition in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM