Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 ngfor only if index is less than certain number

I'm using an ngfor to display a varying amount of items. I want to limit the amount of items it can display to 5 so I use:

<li  *ngFor="let item of items; let i = index;">
     <div *ngIf="i < 4">
       //more stuff ...
     </div>
</li>

But what this does it that it seems to be rendering the space for items over index = 4.

If I try:

<li  *ngFor="let item of items; let i = index;" *ngIf="i < 4"> //Notice ngIF

Nothing is displayed.

Any ideas?

like image 348
CommonSenseCode Avatar asked Dec 18 '22 13:12

CommonSenseCode


1 Answers

You can do it that way:

<li *ngFor="let item of items | slice:0:5; let i = index;">
  <div>
    //more stuff ... 
  </div>
</li>

Maybe that's an option that works for you.

like image 147
JRoppert Avatar answered Dec 29 '22 04:12

JRoppert