got an array with many projects and i want to display the last 3 projects.
got in my html
As answered by Gunter above, you might also need to check the length of "projects" is greater than 4.
<div *ngIf="projects.length>4">
<li *ngFor="let project of projects;">
<h2>{{ project.name }}</h2>
<p>{{ project.meta_desciption }}</p>
</li>
</div>
In my case for getting last three project below code work perfect no need to check projects length it work for any length of array.
<li *ngFor="let project of (projects | slice: -3)">
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.
<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.