【发布时间】:2020-10-05 17:31:45
【问题描述】:
我有一个由对象(id、image)组成的对象数组,并且我还有几张卡片作为我提取到后端的结果。 我想做的是在每张卡片上建立一个轮播,专门使用与卡片实际上具有的 id 匹配的图像,假设这是我的图像数组
arrayOfImages:[
{id: "1",image: "xxxxxxxxxx"},
{id: "1",image: "xxxxxxxxxx"},
{id: "2",image: "xxxxxxxxxx"},
{id: "2",image: "xxxxxxxxxx"},
{id: "1",image: "xxxxxxxxxx"},
{id: "2",image: "xxxxxxxxxx"}
]
让我们说在我的 html 标签上,我触发了 html:
<div *ngFor="let post of allPortafolio;let i=index">
//NgFor that bring me all cards from back
//some code
<ngb-carousel>
//carousel specific for one card where in i want to loop over the array of images
<ng-template ngbSlide *ngFor="let image of arrayOfImages">
<img *ngIf="post.id===image.id;else alternative" [src]="image.image">
<ng-template #alternative>
<img " else src="../../../assets/white.jpg">
</ng-template>
</ng-template>
</ngb-carousel>
</div>
如您所见,必须使用 else ,使用其他固定模板,而循环保持其过程与 id 条件不匹配,但美观是可怕的,因为似乎页面在不断获取充电,而不是在旋转木马中顺畅流动。
但我想知道是否有另一种选择,我可以在不使用 else 的情况下循环该对象数组,并将其各自的图像分配给每张卡片,实际上卡片的 id 匹配,因此它们各自的轮播将流畅无间断
【问题讨论】:
标签: arrays angular for-loop carousel