简体   繁体   English

循环通过 Angular QueryList

[英]Looping through Angular QueryList

So I want to get all the images in my component in an array, so I am using Angular's @ViewChildren which returns a QueryList of ElementRef :所以我想在一个数组中获取我组件中的所有图像,所以我使用 Angular 的@ViewChildren它返回一个QueryListElementRef

@ViewChildren('img', { read: ElementRef }) images: QueryList<ElementRef>;

However, when ever I try to loop through the list am can't ever seem to get hold of the nativeElement .但是,当我尝试遍历列表时,似乎永远无法掌握nativeElement I am calling this in ngAfterViewInit()我在ngAfterViewInit()中调用它

this.images.forEach((img, index) => console.log("Image", img.nativeElement));

I don't get anything in the console.log , no error nothing.我没有在console.log中得到任何东西,没有任何错误。

This in turn does work:这反过来确实有效:

console.log("this.images", this.images);

Below is an image of the log of console.log("this.images", this.images) :下面是console.log("this.images", this.images)的日志图像:

在此处输入图像描述

Here the html :这里是html

 <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>

What am I missing?我错过了什么?

I ran into a similar issue with @ContentChildren .我遇到了与@ContentChildren类似的问题。 The solution was to subscribe to the changes observable of the QueryList .解决方案是订阅QueryList的可观察changes

Essentially, this will trigger whenever any of the state of the children changes.本质上,只要子项的 state 发生任何变化,就会触发。 Normally this wouldn't be necessary but if your list of children is loaded in via a GET, this ensures the QueryList is data is loaded before executing on it.通常这不是必需的,但如果您的子列表是通过 GET 加载的,这可以确保QueryList在执行之前加载数据。 Its important to note the subscription will fire again whenever the any of the children's content changes.重要的是要注意,只要任何孩子的内容发生变化,订阅就会再次触发。

@ContentChildren(forwardRef(() => ChildComponent), { descendants: true })
private children: QueryList<ChildComponent>;

ngAfterContentInit() {
    // subscribe to changes
    this.children.changes.subscribe(children => {
        children.forEach(child => {
            // do something with each child...
        });
    });
}

As @Jakopo Sciampi already mentioned a possibility is also to use the.toArray() function on the QueryList.正如@Jakopo Sciampi 已经提到的那样,也可以在 QueryList 上使用 the.toArray() function。

this.images.toArray().forEach((img, index) => {
            console.log('Image', img.nativeElement);
        });

Just make sure that you don't call the command to early => there will be no elements => the loop is not excecuted.只要确保你没有提前调用命令 => 将没有元素 => 循环没有被执行。

Don't call your loop in ngAfterViewInit , use ngAfterViewChecked instead.不要在ngAfterViewInit中调用你的循环,而是使用ngAfterViewChecked This will be triggered AFTER your child components are checked.这将在检查您的子组件后触发。

See lifecycle-hooks查看生命周期钩子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM