typescript Angular 6 - @ViewChild 访问 nativeElement 跨度类的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50644841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:27:38 来源:igfitidea点击:
Angular 6 - @ViewChild Access value of nativeElement span class
提问by David Anthony Acosta
I am using @ViewChild is Angular.
我正在使用@ViewChild 是 Angular。
On my app.component.htmlI have:
在我的app.component.html 上,我有:
<div #somename>Mark<span class="somethingelse">Hello something else</span></div>
And on my app.component.ts
在我的app.component.ts 上
@ViewChild('somename') SomeName;
constructor() {}
getInputValue() {
alert(this.SomeName.nativeElement('span').getElementByClassName('somethingelse').innerHTML);
}
This is not returning anything.
这不会返回任何东西。
My question is how to I target the span's class and get it's value?
我的问题是如何定位跨度的类并获得它的价值?
回答by David Anthony Acosta
Your view child should be:
你的观点孩子应该是:
@ViewChild("somename") somename: ElementRef;
Then you can do:
然后你可以这样做:
this.somename.nativeElement.firstElementChild.innerHTML
回答by David Anthony Acosta
<div>Mark<span class="somethingelse" #somename>Hello something else</span></div>
@ViewChild('somename') mySpan: ElementRef;
ngAfterViewInit() {
console.log(this.mySpan.nativeElement.innerHTML);
}