简体   繁体   中英

Angular 6 - @ViewChild Access value of nativeElement span class

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?

Your view child should be:

@ViewChild("somename") somename: ElementRef;

Then you can do:

this.somename.nativeElement.firstElementChild.innerHTML
<div>Mark<span class="somethingelse" #somename>Hello something else</span></div>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM