简体   繁体   English

如何在角度4中使用JS属性(例如“ offsetWidth”)

[英]How to use JS property like “offsetWidth” in angular 4

I am trying to get the width of an element using JS in my Angular app. 我试图在Angular应用中使用JS来获取元素的宽度。

document.getElementsByClassName("element")[0].offsetWidth;

When I do this i am getting the below compilation error 当我这样做时,我得到以下编译错误

Property 'of fsetWidth' does not exist on type 'Element'. 类型“元素”上不存在属性“ fsetWidth”。

Could someone tell me how to use it in Angular 4. Thanks 有人可以告诉我如何在Angular 4中使用它。谢谢

If give identification of your DOM element using # 如果使用#给出您的DOM元素的标识

Template: 模板:

<div class="for-example" #titleText>For Example</div>

Component: 零件:

Declare 宣布

@ViewChild('titleText') titleTextElement;

and get offsetWidth like following: 并获得offsetWidth,如下所示:

this.titleTextElement.nativeElement.offsetWidth

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

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