Madasu K
Madasu K

Reputation: 1863

angular ngFor with condition

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>

Upvotes: 0

Views: 5505

Answers (2)

Gokulan P H
Gokulan P H

Reputation: 1898

<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>

Upvotes: 4

Jayakrishnan
Jayakrishnan

Reputation: 4294

You need to simply use slice.

<li *ngFor="let a of sampleArray | slice:0:9">
  <p>{{a.firstname}}</p>
  <p>{{a.lastname}}</p>
</li>

Upvotes: 6

Related Questions