要获得您想要的效果,您需要利用 css 创建一个masonry layout。
最好的方法是使用 css column-count,但您需要转置数组,以便数据的每一行代表一列。
此示例利用 ngClass 提供一个对象,其中键是 CSS 类,当值中给出的表达式计算为 true 时,这些类会被添加。
masonry.component.ts
export class MasonryComponent {
wall = [['T', 'T', 'T'], ['S', 'A', 'S'], ['T', 'S', 'T']];
constructor() { }
transpose(arr: any[]){
return arr.map((col, i) => arr.map((row) => row[i] ));
}
}
masonry.html
<div class="wall" >
<ng-container *ngFor="let blockCol of transpose(wall)">
<div *ngFor="let block of blockCol" class="block" [ngClass]="{
'tall' : block === 'T',
'short': block === 'S',
'special': block === 'A' }">{{ block }}</div>
</ng-container>
</div>
masonry.css
.wall {
width: 170px;
column-count: 3;
}
.block {
width: 50px;
border: 1px solid black;
}
.tall { height: 50px; }
.special { height: 50px; }
.short { height: 25px; }