I am currently trying to gather all the images in my component and store them in an array. To achieve this, I am utilizing Angular's @ViewChildren
which returns a QueryList
of ElementRef
:
@ViewChildren('img', { read: ElementRef }) images: QueryList<ElementRef>;
However, I am facing an issue while attempting to access the nativeElement
when looping through the list. The code snippet below exemplifies this scenario within the ngAfterViewInit()
method:
this.images.forEach((img, index) => console.log("Image", img.nativeElement));
Surprisingly, there is no output or error message displayed in the console log.
In contrast, when logging only the this.images
, it functions correctly:
console.log("this.images", this.images);
For reference, an image showing the result of
console.log("this.images", this.images)
can be found here:
https://i.sstatic.net/t1zIw.png
The corresponding HTML structure is provided below:
<div class="swiper-slide" *ngFor="let exhibit of exhibits; let i = index">
<app-exhibit-images exhibitImagesId="{{ exhibit.fields.eventImages[0].sys.id }}" [eventPicturesStart]="eventPicturesStart" (sendEndEventPictures)="getEndEventImages($event)"></app-exhibit-images>
<div id="exhibit-{{i}}"class="gallery__content" [ngClass]="{'show-event-pictures' : eventPicturesStart}">
<div class="gallery__poster" [ngClass]="{'gallery-expand' : isGalleryExpanded }">
<figure class="poster__image">
<picture>
<source srcset="{{ exhibit.fields.photo.fields.file.url }}" type="img/png">
<img #img src="{{ exhibit.fields.photo.fields.file.url }}" alt="{{ exhibit.fields.photo.fields.description }}">
</picture>
</figure>
</div>
<div class="gallery__text" [ngClass]="{'gallery-expand' : isGalleryExpanded }">
<button class="gallery-expand-button" (click)="expandGallery()"></button>
<h2 class="gallery__heading">{{ exhibit.fields.title }}</h2>
<h3 class="gallery__subheading">{{ exhibit.fields.subTitle }}</h3>
<h4 class="gallery__artist">{{ exhibit.fields.artist }}</h4>
<p class="gallery__description" *ngIf="exhibit.fields.description">{{ exhibit.fields.description }}</p>
</div>
</div>
</div>
Am I overlooking something crucial?