Like document.getElementById("id") in javascript, what we can do in angular2 and above versions to get dom element in type script?
I tried with angular.element but its not working
Like document.getElementById("id") in javascript, what we can do in angular2 and above versions to get dom element in type script?
I tried with angular.element but its not working
 
    
    There are multiple ways to do that. I am going to show one of them.
You can use this.
the working example is here
Component.ts
import { Component, ViewChild, AfterViewInit, ElementRef } from '@angular/core';
import { HelloComponent } from './hello.component';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements AfterViewInit {
  name = 'Angular';
  @ViewChild('pRef', {static: false}) pRef: ElementRef;
    ngAfterViewInit() {
    console.log(this.pRef.nativeElement.innerHTML); 
    this.pRef.nativeElement.innerHTML = "DOM updated succesfully!!!"; 
  }
}
.html
<hello name="{{ name }}" ></hello>
<p #pRef>
  Start editing to see some magic happen :)
</p>
The working example is here
You can find more info about ViewChild and it's usage here: https://angular.io/api/core/ViewChild
 
    
     
    
    It is against Angular's guidelines to directly modify the DOM.
Instead use ViewChild(ren)() selector and modify the element using the Renderer2 service.
Ex:
<div #myDiv> </div>
...
@ViewChild('myDiv',{static:true}) myDiv:ElementRef;
constructor(private renderer:Renderer2){
this.renderer.setStyle(this.myDiv.elementRef,'style','value');
}
