简体   繁体   English

ngFor 创建不需要的 ng-template 副本

[英]ngFor creates unwanted copies of ng-template

I have an issue with ngFor directive, and I just can't find a way around it:我对 ngFor 指令有疑问,只是找不到解决方法:

<ng-container *ngIf="user.images.length > 0">
   <div *ngFor="let image of images">
        <img
           *ngIf="image.isProfileImage; else noImage"
           [src]="image.path"
        />
   </div>
</ng-container>
                    
<ng-template #noImage>
   <img src="https://someplaceholder.com"/>
</ng-template>

So, if the image is "profile image", use it.因此,如果图像是“个人资料图像”,请使用它。 If not, use the template with the placeholder image.如果没有,请使用带有占位符图像的模板。 In some cases, if there are more images in the array and none of them is a profile image, it shows the ng-template but duplicated many times (by number of images in the array)..., but it should show it only once and "break out" of the loop.在某些情况下,如果数组中有更多图像并且它们都不是个人资料图像,它会显示ng-template但重复多次(按数组中的图像数)...,但它应该只显示它一次并“跳出”循环。

Am I missing something very obvious?我错过了一些非常明显的东西吗?

There is no way to break out of an ngFor loop.没有办法跳出 ngFor 循环。 However, there is a way to solve your problem.但是,有一种方法可以解决您的问题。

You can create a method in your component that will loop over the array and check if there's a profile image there, then you can alter your HTML and you check wether the array contains a profile image or not.您可以在组件中创建一个方法,该方法将遍历数组并检查那里是否有配置文件图像,然后您可以更改 HTML 并检查数组是否包含配置文件图像。 If not, then you can just show the ngTemplate.如果没有,那么您可以只显示 ngTemplate。

Example例子


<ng-container *ngIf="user.images.length">
<ng-container *ngIf="containsProfileImage(); else noImage">
   <div *ngFor="let image of images">
        <img
           *ngIf="image.isProfileImage"
           [src]="image.path"
        />
   </div>
</ng-container>
</ng-container>
                    
<ng-template #noImage>
   <img src="https://someplaceholder.com"/>
</ng-template>


containsProfileImage(): boolean{
   let returnVal = false;
   
   this.user.images.forEach(image => {
      if (image.isProfileImage) {
         returnVal = true;
         return;
      }
   }

   return returnVal;
}

EDIT: Alternative solution编辑:替代解决方案

As Chris G said as a response.正如Chris G作为回应所说的那样。 You can also filter your images by the images that have profile images.您还可以通过具有个人资料图像的图像过滤您的图像。 This way you can check whether there is atleast one image in the filtered array.通过这种方式,您可以检查过滤后的数组中是否至少有一个图像。 If not, show the noImage ngTemplate.如果没有,请显示noImage ngTemplate。

<ng-container *ngIf="user.images.length">
<ng-container *ngIf="filteredImagesLength; else noImage">
   <div *ngFor="let image of images">
        <img
           *ngIf="image.isProfileImage"
           [src]="image.path"
        />
   </div>
</ng-container>
</ng-container>
                    
<ng-template #noImage>
   <img src="https://someplaceholder.com"/>
</ng-template>
get filteredImagesLength(): number {
   return user.images.filter(i => i.isProfileImage)?.length;
}

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

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