0

In my angular 4 application I need to use ngFor with less than conditon. I mean I do not want to show all the item of sampleArray, instead I want to display only first 10 items, just like in normal java script we have i < sampleArray.length or i < 10, etc these kind of conditions I want to use in ngFor, is it possible?

<li *ngFor="let a of sampleArray">
    <p>{{a.firstname}}</p>
    <p>{{a.lastname}}</p>
  </li>
Madasu K
  • 1,813
  • 2
  • 38
  • 72
  • there already an answer for the same check this link https://stackoverflow.com/questions/37818677/how-can-i-limit-ngfor-repeat-to-10-items-in-angular-2 – Rahul Singh Jun 30 '17 at 11:57
  • 1
    Possible duplicate of [How can I limit ngFor repeat to 10 items in Angular 2?](https://stackoverflow.com/questions/37818677/how-can-i-limit-ngfor-repeat-to-10-items-in-angular-2) – developer033 Jun 30 '17 at 12:47

2 Answers2

6

You need to simply use slice.

<li *ngFor="let a of sampleArray | slice:0:9">
  <p>{{a.firstname}}</p>
  <p>{{a.lastname}}</p>
</li>
Jayakrishnan
  • 4,232
  • 2
  • 23
  • 35
4
<li *ngFor="let a of sampleArray;  let i=index">
    <div *ngIf="i<2">
        <p>{{a.firstname}}</p>
        <p>{{a.lastname}}</p>
    </div>
</li>

Updated:

<ng-container *ngFor="let a of sampleArray;  let i=index">
  <li *ngIf="i<11">
    <p>{{a.firstname}}</p>
    <p>{{a.lastname}}</p>
  </li>
</ng-container>
Gokulan P H
  • 1,838
  • 1
  • 10
  • 13