本文介绍了在angular4/打字稿中替换document.getElementById?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我在练习工作中使用了angular4,这对我来说是新的.幸运的是,为了获取html元素及其值,我使用了<HTMLInputElement> document.getElementById
或<HTMLSelectElement> document.getElementById
So, im working with angular4 in my practice work and this is new for me.Luckyly, in order to get html elements and its values i used <HTMLInputElement> document.getElementById
or <HTMLSelectElement> document.getElementById
我想知道是否可以用angular
Im wondering if there is any replacement for this in angular
推荐答案
您可以使用#someTag
标记DOM元素,然后使用@ViewChild('someTag')
进行获取.
You can tag your DOM element using #someTag
, then get it with @ViewChild('someTag')
.
查看完整示例:
import {AfterViewInit, Component, ElementRef, ViewChild} from '@angular/core';
@Component({
selector: 'app',
template: `
<div #myDiv>Some text</div>
`,
})
export class AppComponent implements AfterViewInit {
@ViewChild('myDiv') myDiv: ElementRef;
ngAfterViewInit() {
console.log(this.myDiv.nativeElement.innerHTML);
}
}
console.log
将打印一些文本.
这篇关于在angular4/打字稿中替换document.getElementById?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!