【发布时间】:2020-12-01 12:15:04
【问题描述】:
使用ngFor 仅对列表的每 n 个倍数进行迭代的最佳方法是什么?目前,我将在.ts 文件中获取所有n 倍数并在ngFor 中使用它们。有没有更好的方法,比如在模板中生成这些倍数,ngFor 或任何其他更好的简化方法。
-- * .ts 文件 *--
ngOnInit(): void {
this.req_Indeces=[];
this.incidentCount = this.incidentList.length;
for(var i=0;(n*i)<this.incidentCount;i++){
this.req_Indeces.push(n*i);
}
console.log(this.req_Indeces);
}
-- * .html 文件 *--
<ul *ngFor="let p of req_Indeces;let i=index; first as isFirst;let x=count">
<li >{{incidentList[p].src}}</li>
<!-- (ignore this part if don't want to go into more details) adding rest of ele between multiples -->
<li >{{incidentList[p].src}}</li>
<li >{{incidentList[(p+1)%(n*x)].src}}</li>
<li >{{incidentList[(p+2)%(n*x)].src}}</li>
.
<li >{{incidentList[(p+(n-1))%(n*x)].src}}</li>
</ul>
更多限制:
- 使用
ngIf并且跳过不是一种选择,因为它会在要显示的请求列表之间给出空列表。 - 为简单起见,我认为
incidentCount,n是均匀的。
【问题讨论】: