Angular 6 - @ViewChild 访问 nativeElement span 的值 class

Angular 6 - @ViewChild Access value of nativeElement span class

我正在使用@ViewChild Angular。

在我的 app.component.html 我有:

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

还有我的 app.component.ts

@ViewChild('somename') SomeName;

  constructor() {}

  getInputValue() {
 alert(this.SomeName.nativeElement('span').getElementByClassName('somethingelse').innerHTML);
  }

这没有返回任何东西。

我的问题是如何定位跨度的 class 并获取它的值?

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

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

您的视图子应该是:

@ViewChild("somename") somename: ElementRef;

那么你可以这样做:

this.somename.nativeElement.firstElementChild.innerHTML