Show only first match from *ngIf expression

醉酒当歌 提交于 2020-01-26 04:42:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!