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>
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>
Share
Improve this question
asked Jun 30, 2017 at 11:50
Madasu KMadasu K
1,8632 gold badges39 silver badges76 bronze badges
2
- there already an answer for the same check this link stackoverflow./questions/37818677/… – Rahul Singh Commented Jun 30, 2017 at 11:57
- 1 Possible duplicate of How can I limit ngFor repeat to 10 items in Angular 2? – developer033 Commented Jun 30, 2017 at 12:47
2 Answers
Reset to default 6You need to simply use slice.
<li *ngFor="let a of sampleArray | slice:0:9">
<p>{{a.firstname}}</p>
<p>{{a.lastname}}</p>
</li>
<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>