问题
How can I show only one (first) match from the *ngIf.
I have an object loop with *ngFor following with *ngIf expression containing items with same Id's and different dates. I want to filter and show only the one with most recent date, not duplicating them, since I filter by objectId.
回答1:
In html file:
<div *ngFor="let item of list">
<div *ngIf="item.id == matchWithCondion ?func():false">
Add your code here
</div>
</div>
In typescript file initialize a variable as:
let isFirstMatch = false;
constructor(){}
func() {
if (!isFirstMatch) {
this.isFirstMatch = true;
return true;
} else {
return false;
}
}
In html file:
<div *ngFor="let item of list">
<div *ngIf="item.id == matchWithCondion ? func() : false">
Add your code here
</div>
</div>
In typescript file initialize a variable as:
let isFirstMatch = false;
来源:https://stackoverflow.com/questions/57572202/show-only-first-match-from-ngif-expression