How can I limit ngFor repeat to some number of items in Angular?
For example, lets say we want to display only the first 10 items of an array, we could do this using the SlicePipe like so:
<ul>
<li *ngFor="let item of items | slice:0:10">
{{ item }}
</li>
</ul>
This seems simpler to me
<li *ngFor="let item of list | slice:0:10; let i=index" class="dropdown-item" (click)="onClick(item)">{{item.text}}</li>
Closer to your approach
<ng-container *ngFor="let item of list; i as index">
<li class="dropdown-item" (click)="onClick(item)" *ngIf="i<11">{{item.text}}</li>
</ng-container>
alternative
<ng-template ngFor let-item [ngForOf]="list" let-i="index">
<li class="dropdown-item" (click)="onClick(item)" *ngIf="i<11">{{item.text}}</li>
</ng-template>
You can directly apply slice()
to the variable. StackBlitz Demo
<li *ngFor="let item of list.slice(0, 10);">
{{item.text}}
</li>
This works very well:
<template *ngFor="let item of items; let i=index" >
<ion-slide *ngIf="i<5" >
<img [src]="item.ItemPic">
</ion-slide>
</template>