是否有内置管道仅显示数组中的部分条目

Is there a built-in pipe to show only part of entries in an array

我正在展示产品:

<li *ngFor="let product of products">{{product.id}}</li>

我想限制使用组件上的 属性 显示的条目数。是否有内置管道可以做到这一点,或者我应该创建自己的管道?

这是我的看法:

<li *ngFor="let product of products | length[propertyOnComponent]">{{product.id}}</li>

因此,如果 propertyOnComponent 为 3,则只会显示 3 个条目。

实现这一点的最佳方法是使用带有 startend 参数的 slice pipe

<li *ngFor="let product of products | slice:0:propertyOnComponent">
  {{product.id}}
</li>

参见 https://angular.io/docs/ts/latest/guide/pipes.html

中的 slice
<li *ngFor="let product of products | slice:0:propertyOnComponent">{{product.id}}</li>