Попробуйте это в Angular 7
import {AfterViewInit, Component, ElementRef, ViewChild,Renderer2,OnInit} from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
constructor(private renderer: Renderer2, private el: ElementRef) {}
id='';
ngOnInit() {
this.id = this.el.nativeElement.getElementsByClassName('mydiv')[0];
console.log(this.id);
}
}
<p class="mydiv">
Start editing to see some magic happen :)
</p>