document.getElementById replacement in angular4 / typescript?

后端 未结 5 772
刺人心
刺人心 2020-11-29 03:10

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 documen

5条回答
  •  天命终不由人
    2020-11-29 03:35

    You can tag your DOM element using #someTag, then get it with @ViewChild('someTag').

    See complete example:

    import {AfterViewInit, Component, ElementRef, ViewChild} from '@angular/core';
    
    @Component({
        selector: 'app',
        template: `
            
    Some text
    `, }) export class AppComponent implements AfterViewInit { @ViewChild('myDiv') myDiv: ElementRef; ngAfterViewInit() { console.log(this.myDiv.nativeElement.innerHTML); } }

    console.log will print Some text.

提交回复
热议问题