简体   繁体   English

在不使用 nativeElement 的情况下从 @ViewChild 读取元素

[英]Read an element from the @ViewChild without using nativeElement

Is there anyway to get the underlying div element on a @ViewChild ?无论如何要在@ViewChild上获取底层 div 元素? I have tried to read the following of which none work:我试图阅读以下内容,但其中没有任何作用:

  • ElementRef.prototype.nativeElement -- returns ElementRef ElementRef.prototype.nativeElement -- 返回ElementRef
  • HTMLDivElement -- returns undefined HTMLDivElement -- 返回undefined
  • ElementRef -- returns ElementRef ElementRef -- 返回ElementRef

I am trying to do this so I can do without having to call nativeElement in every call to the content property.我正在尝试这样做,这样我就可以不必在每次调用content属性时调用nativeElement

Here is what I am doing (replacing the read with an item from the list above):这是我正在做的事情(用上面列表中的项目替换read ):

  @ViewChild('overlayContent', { read: ElementRef.prototype.nativeElement })
  content?: HTMLDivElement;
<div class="slide-over-content" #overlayContent>
  <!-- content -->
</div>

The common pattern is to use a setter if one more level of abstraction is needed between an ElementRef and its property.如果在 ElementRef 和它的属性之间需要多一层抽象,常见的模式是使用 setter。

 @ViewChild('overlayContent')
    set aRef(ref: ElementRef) {
      this.myDomElement = ref.nativeElement;
    }

暂无
暂无

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

相关问题 使用@viewchild 时,无法以角度读取未定义的属性“nativeElement” - Cannot read property 'nativeElement' of undefined in angular when using @viewchild Angular 4 无法使用 ViewChild 读取未定义的属性 nativeElement - Angular 4 cannot read property nativeElement of undefined using ViewChild 类型错误:无法使用@ViewChild 读取未定义的属性“nativeElement”? - TypeError: Cannot read property 'nativeElement' of undefined with @ViewChild? 如何解决使用viewchild ElementRef时无法读取角度未定义的属性&#39;nativeElement&#39; - How to solve Cannot read property 'nativeElement' of undefined in angular when using viewchild ElementRef 使用Angular中的ViewChild()获取元素 - Getting an element using ViewChild() in angular 在不使用 TypeScript 的情况下使用 @ViewChild - Using @ViewChild without using TypeScript 无法从Angular 4中的viewChild元素调用函数 - Cannot call function from viewChild element in Angular 4 当您使用来自“@ViewChild”的信息时,如何避免“ExpressionChangedAfterItHasBeenCheckedError”或“无法读取未定义的属性”? - How to avoid "ExpressionChangedAfterItHasBeenCheckedError" or "Cannot Read Property of Undefined" when you're using info from "@ViewChild"? 从Angular 4中的主机元素访问子主机元素nativeElement属性 - Access child host elements nativeElement properties from host element in angular 4 查询 Angular 中的 Dom 元素(DIV id 或类)但没有 @ViewChild - Query Dom element (DIV id or class) In Angular but without @ViewChild
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM