我有2列列表。是否可以添加数组元素是奇数元素,如果它是偶数元素则可以关闭标记。
<ng-container ngFor="let item of items; let odd=odd;">
<!-- if odd -->
<div class="item-wrapper">
<!-- /if -->
<div class="item">{{item.name}}</div>
<!-- if !odd -->
</div>
<!-- /if -->
</ng-container>
在knockout.js中,我可以这样做:
<!-- ko foreach: items -->
<!-- ko if: $i % 2 !== 0 -->
<div class="item-wrapper">
<!-- /ko -->
<div class="item">{{items.name}}</div>
<!-- ko if: $i % 2 === 0 -->
</div>
<!-- /ko-->
<!-- /ko-->
布局的最终结果应该是:
<div class="item-wrapper">
<div class="item">#NAME_1</div>
<div class="item">#NAME_2</div>
</div>
<div class="item-wrapper">
<div class="item">#NAME_3</div>
<div class="item">#NAME_4</div>
</div>
答案 0 :(得分:1)
@Pipe({name: 'split'})
export class BundlePipe implements PipeTransform {
transform(val, size) {
if(!size || !val || !val.length) {
return val;
}
var result = [[]];
var row = 0;
for(int i = 0; i < val.length; i++) {
result[row].push(val[i]);
if(i % row === 0) {
row++;
}
}
}
}
<ng-container ngFor="let item of items | split:2; let odd=odd;">
<div class="item-wrapper"></div>
<div *ngFor="let p of item" class="item">{{item.name}}</div>
</div>
</ng-container>
警告 - 未经测试 - 只是为了证明这个想法