最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angular ngFor with condition - Stack Overflow

programmeradmin0浏览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>

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
Add a ment  | 

2 Answers 2

Reset to default 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>
<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>
发布评论

评论列表(0)

  1. 暂无评论