Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 615
Avinash Raj Avatar asked Oct 28 '25 17:10

Avinash Raj


1 Answers

You can use the slice pipe for that

<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

like image 192
Günter Zöchbauer Avatar answered Oct 31 '25 06:10

Günter Zöchbauer