Angular 6 - @ViewChild Access value of nativeElement span class

此生再无相见时 提交于 2020-04-16 05:08:07

问题


I am using @ViewChild is Angular.

On my app.component.html I have:

<div #somename>Mark<span class="somethingelse">Hello something else</span></div>

And on my 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?


回答1:


Your view child should be:

@ViewChild("somename") somename: ElementRef;

Then you can do:

this.somename.nativeElement.firstElementChild.innerHTML



回答2:


<div>Mark<span class="somethingelse" #somename>Hello something else</span></div>

@ViewChild('somename') mySpan: ElementRef;
ngAfterViewInit() {
  console.log(this.mySpan.nativeElement.innerHTML);
}


来源:https://stackoverflow.com/questions/50644841/angular-6-viewchild-access-value-of-nativeelement-span-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!