I want to select a specific html element that created dynamically. But "list" variable which create the html blocks is filling with http request and dom not loaded immediately. I want to select the element via id that got from url and equal to list item's id. So selected element is undefined even if I select the element in ngAfterViewInit hook. How can I select?
HTML
<div id="wrapper">
<ng-container *ngFor="let item of list">
<div [id]="item.id">{{item.content}}</div>
</ng-container>
</div>
TS
list = null;
selectedItemId = null;
ngOnInit() {
this.selectedItemId = this.activatedRoute.snapshot.params.id;
}
getList() {
this.http.get('http://api.example.com').subscribe(
result => this.list = result
)
}
ngAfterViewInit() {
const htmlElement= document.getElementById(this.selectedItemId);
/* Some codes that using htmlElement variable */
}
*ngIf="list"on the wrapper block