您可以通过将原始数组中的数据分块到一个二维数组中来做到这一点,其中第 0 项是包含前 30 项的数组,第 1 项是包含后 30 项等的数组。例如,如果您的数组是:
['this', 'is', 'some', 'data', 'that', 'will', 'be', 'rendered', 'in', 'the', 'cards']
分块数组(假设块大小为 3)将是:
[['this', 'is', 'some'], ['data', 'that', 'will'], ['be', 'rendered', 'in'], ['the', 'cards']]
我已经实现了以下函数来分块数据(只需将3 更改为您想要的任何块大小):
readonly chunkSize: number = 3;
getChunkedData() {
var chunkedData = [];
for (let i = 0; i < this.data.length; i += this.chunkSize) {
chunkedData.push(this.data.slice(i, i + this.chunkSize));
}
return chunkedData;
}
然后您可以使用ngFor 循环分块数组并将每个项目呈现在一张卡片中:
<mat-card *ngFor="let data of getChunkedData()">
<p *ngFor="let item of data">
{{item}}
</p>
</mat-card>
请参阅this StackBlitz 以获得工作演示。