I can’t get the "box-message" class using "document.querySelectorAll(‘.box-message’)" inside a tree where the "*ngFor" directive is used.
I am making an "http rest" request, after the request the "*ngFor" directive completes the building of the house and after that I am trying to get the "box-message" class using "document.querySelectorAll(‘.box-message’)".
But as I understand it (but I could be wrong), the house does not have time to be built, and that is why I do not receive the “box-message” class.
I tried using AfterViewInit but it doesn’t help.
How to solve this problem?
HTML
<ul class="ul-ads">
<li class="li-ads" *ngFor="let ads of ads">
<div class="box-message">{{ads.message}}</div>
</li>
</ul>
SCRIPT
export class AdsComponent implements OnInit {
ads!: Ads[];
public getAds(): Observable<any> {
return this.http.get<any>(`http://localhost:8080/apis/data/ads`,
{observe: 'response'});
}
ngOnInit(): void {
this.getAds().subscribe({
next: data => {
this.ads = responseData.body.content;
const boxMessage = document.querySelectorAll('.box-message');
console.log(boxMessage) // NodeList[]
}
});
}
}
2
Answers
You can leverage ViewChildren to accomplish this.
In your template. add a reference for each box message:
In your component, add a property to hold the view children and subscribe to the
changes
observable:Any time the view children are changed, your code will execute after the children are rendered.
There’s no need to use setTimeout to introduce an artificial delay.
Hope that helps you out.
You need "wait" Angular repaint. Generally You use a setTimeout without miliseconds.
Think that angular execute the instructions under subscribe and then repaint. It’s the reason you not get the elements.
IMPORTANT: In Angular rarely we need use the old javascript way document.getElementBy. You can declare ViewChildren using a template reference variable
Then you can get all of them in a QueryList
See that in an elementRef, the
nativeElement
property is the html tagA Query list allow convert to Array, reorder, get the first and last element, subscribe to changes…