[英]How to limit number of items to get displayed in ngFor?
I want the display only the first 3 items from a list using *ngFor
我想只使用
*ngFor
显示列表中的前3项
On my component, 在我的组件上,
formInputs: any = [
{name:'foo'},
{name: 'bar'},
{name: 'buzz},
{name: 'dhsfk'},
{name: 'sadsd'}
]
On my template, 在我的模板上,
<div class="form-group label-floating" *ngFor="let input of formInputs">
{{input.name}}
</div>
And note that, I want to apply the change only in the template itself not in the component. 请注意,我只想在模板中应用更改,而不是在组件中应用更改。
You can use the slice
pipe for that 您可以使用
slice
管道
<div class="form-group label-floating" *ngFor="let input of formInputs | slice:0:3">
{{input.name}}
</div>
https://angular.io/docs/ts/latest/api/common/index/SlicePipe-pipe.html https://angular.io/docs/ts/latest/api/common/index/SlicePipe-pipe.html
Creates a new List or String containing a subset (slice) of the elements.
创建包含元素子集(切片)的新List或String。
array_or_string_expression | slice:start[:end]
*ngFor="let input of formInputs | slice:0:3"
Change your code: 更改您的代码:
<div class="form-group label-floating" *ngFor="let input of formInputs | slice:0:3">
{{input.name}}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.