【发布时间】:2018-04-26 17:46:06
【问题描述】:
我想在li 标记中打印一组项目。
例如,该数组将有 10 个元素,但我只想在移动设备上打印 4 个,在桌面上打印 6 个。
我不想使用slice 方法删除非打印元素,因为将来它应该是一个滑块。
*ngFor 可以解决这个问题吗?
这是我的 HTML
<div class="container">
<ul>
<li *ngFor="let item of items">
<div class="producers">
{{ item }}
</div>
</li>
</ul>
</div>
还有我的 TS
export class ProducersComponent implements OnInit {
items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'];
innerWidth
itemsToPrint
@HostListener('window:resize', ['$event'])
onResize(event) {
this.innerWidth = window.innerWidth;
if (this.innerWidth < 600) {
this.itemsToPrint = 4
} else {
this.itemsToPrint = 6
}
}
constructor() { }
ngOnInit() {
this.onResize(event)
}
}
我正在检查窗口大小以更改要打印的项目数。
如何在 HTML 文件中使用它?
干杯,
库巴
【问题讨论】:
-
您可以使用管道过滤值。
标签: angular