简体   繁体   English

有条件的ngFor

[英]angular ngFor with condition

In my angular 4 application I need to use ngFor with less than conditon. 在我的angular 4应用程序中,我需要使用小于条件的ngFor。 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? 我的意思是我不想显示所有的sampleArray项目,而是只显示前10个项目,就像在普通的Java脚本中一样,我们有i < sampleArray.lengthi < 10 ,等等。在ngFor中使用,可能吗?

<li *ngFor="let a of sampleArray">
    <p>{{a.firstname}}</p>
    <p>{{a.lastname}}</p>
  </li>

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM