Doesn’t this return all the elements in the DOM? Is there a way how to return only elements generated by the Angular component I’m “in”?
You need to…
-
Inject
ElementRefin theconstructorconstructor(private renderer: Renderer, private elem: ElementRef){} -
Find the elements you are searching using
querySelectorAllapi.ngAfterViewInit(){ // you'll get your through 'elements' below code let elements = this.elem.nativeElement.querySelectorAll('.classImLookingFor'); }
The answer @Aravind has provided is not the best for the performance as it will search the whole DOM.
This solution will just search the DOM inside the current component.