[英]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.