Need to show counter inside ngif in Ionic

☆樱花仙子☆ 提交于 2019-12-23 05:24:15

问题


I am fetching data from API on HTML using ngFor, but filtering with ngIf, see below home.html code.

{{list.i}} is showing me the array position number (17,18,19...), but I want to show 1,2,3,4.., how do I do that?

 <ion-list no-lines *ngFor="let list of display;let i=index;" >
        <ion-item  text-wrap *ngIf="list.ENTUSR='kiran'">
{{list.i}}
    {{list.ENTUSR}}
    {{list.DESCRPITION}}
    </ion-item> 
    </ion-list>

回答1:


I think here it is what you need

Component Side :

getUsers(users , name){
    return users.filter((user) => user.name === name);
}

Template Side :

<div *ngFor="let user of getUsers(users,'Vivek');let i = index;">
  index -> {{ i + 1 }} <br/> 
  id -> {{ user.id}} <br/>
  <hr/>
</div>

WORKING DEMO




回答2:


You should compare as ===

 <ion-item  text-wrap *ngIf="list.ENTUSR==='kiran'">

and

<ion-list no-lines *ngFor="let list of display;let i=index;">
    <ng-container *ngFor="let each of list.i" *ngIf="list.ENTUSR='kiran'">
        <ion-item text-wrap>
            {{i}} {{list.ENTUSR}} {{list.DESCRPITION}}
        </ion-item>
    </ng-container>
</ion-list>



回答3:


In constructor or when you populate display array filter data with that condition and then loop thrue filtered data:

displayKiran = [];

constructor() {
  this.displayKiran = this.display.filter(dis => {
     return dis.name == 'kiran';
  });
}

<ion-list no-lines *ngFor="let list of displayKiran;let i=index;" >
    <ion-item  text-wrap>
        {{list.i}} {{list.ENTUSR}} {{list.DESCRPITION}}
    </ion-item> 
</ion-list>


来源:https://stackoverflow.com/questions/48560921/need-to-show-counter-inside-ngif-in-ionic

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