简体   繁体   English

打字稿中是否有“document.getElementById('first')”的替代方案?

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

相关问题 angular4 / typescript中的document.getElementById替换? - document.getElementById replacement in angular4 / typescript? 打字稿中的 document.getElementById(s).document.getElementsByClassName 错误 - document.getElementById(s).document.getElementsByClassName error in typescript typescript子组件document.getElementById返回null - typescript child component document.getElementById return null Angular document.getElementById 不能动态使用插值 - Angular document.getElementById not working with interpolation dynamically 替换 Angular 中的 document.getElementById('checkBox') - Replacing document.getElementById('checkBox') in Angular 如何通过 document.getElementById inAngular 更改显示 - How to change display by document.getElementById inAngular Angular 12 对象在 document.getElementById(elementId) 中可能为“null” - Angular 12 Object is possibly 'null' in document.getElementById(elementId) document.getelementbyid 始终在 angular 13 中返回 null - document.getelementbyid always returns null in angular 13 未调用的函数添加了document.getElementById(&#39;someId&#39;)。appendChild() - Function not calling added with document.getElementById('someId').appendChild() 带有 Nrwl/Nx 的 Angular SSR - 外部库调用 document.getElementById - Angular SSR with Nrwl/Nx - external library calls document.getElementById
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM