Following has an interaction example as well.
HTML
<p #interestedElement>
Initial Content
</p>
<button (click)="changeContent()">Press To Change Content</button>
Component
import { Component, OnInit, ViewChild, AfterViewInit, ElementRef, Renderer2 } from
'@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit, AfterViewInit {
@ViewChild('interestedElement') element: ElementRef
constructor(private renderer: Renderer2) {
}
ngOnInit() {
}
ngAfterViewInit() {
console.log('ViewChild', this.element.nativeElement.innerText)
}
changeContent() {
this.renderer.setProperty(this.element.nativeElement, 'innerText', 'Changed Text')
// Yes you can do the following as well but the recommended method is above
// this.element.nativeElement.innerText = 'Changed Text'
}
}
Example
https://stackblitz.com/edit/angular-ivy-flthju
Some Renderer2 related documentation
https://www.digitalocean.com/community/tutorials/angular-using-renderer2