[英]Is there any alternative of `document.getElementById('first')` in typescript?
As per my knowledge document.getElementById('first')
is used in JavaScript.根据我的知识document.getElementById('first')
在 JavaScript 中使用。 and I want to know if there is any other way to access a particular div with it's id in Typescript for Angular-8.我想知道是否还有其他方法可以访问特定 div,它的 id 在 Typescript for Angular-8 中。 Kindly let me know, It would be very helpful for me.请让我知道,这对我很有帮助。
You can use @ViewChild()
to refer template elements in typescript.您可以使用@ViewChild()
在打字稿中引用模板元素。
In your template HTML:在您的模板 HTML 中:
<div #MyDiv>Hello World</div>
In your component.ts:在你的 component.ts 中:
@ViewChild("MyDiv") div: ElementRef;
changeContent() {
this.div.nativeElement.innerHTML = "text";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.