got an array with many projects and i want to display the last 3 projects.
got in my html
<li *ngFor="let project of projects;">
<h2>{{ project.name }}</h2>
<p>{{ project.meta_desciption }}</p>
</li>
it s displaying all the project now (over 20). how can i display only the last 3? I think i need to use "last" somewere in my code, but can't figure it out https://angular.io/docs/ts/latest/api/common/index/NgFor-directive.html
<li *ngFor="let project of (projects | slice:projects.length - 4);">
You might need some additional null
check for projects
or ensure that it is always an array.
Gunter's answer above didn't work in my case. I was able to accomplish the same thing but slightly different:
<ng-container *ngFor="let project of projects; let i = index">
<li *ngIf="i > projects.length - 4">
<h2>{{ project.name }}</h2>
<p>{{ project.meta_desciption }}</p>
</li>
</ng-container>
If the index of the loop is greater then the length of the array minus 4. So if the length is less then 3 it prints all, if it is 3 or greater it just prints the last 3 in whatever order they come in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With