3

I'm showing products:

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

I want to limit the number of entries shown using the property on component. Is there a built-in pipe to do that or I should create my own pipe?

Here is how I see it:

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

So, if propertyOnComponent is 3, then only 3 entries will be shown.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

2 Answers2

5

The best way to implement this is to use the slice pipe with start and end parameter.

<li *ngFor="let product of products | slice:0:propertyOnComponent">
  {{product.id}}
</li>
muetzerich
  • 5,600
  • 7
  • 37
  • 52
5

See slice in https://angular.io/docs/ts/latest/guide/pipes.html

<li *ngFor="let product of products | slice:0:propertyOnComponent">{{product.id}}</li>
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567