Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 3 get index of clicked item

I need to get the index of the clicked item in an ion list, so I can access the position in an array.

The html code I used is this:

<ion-list>
 <ion-item *ngFor="let poi of poiList" (click)="openPage(poi, $index)">
 <h2> {{ poi.name }} </h2>
</ion-item>
</ion-list>

Inside the function openPage I've printed the index in the console, but it is showed as "undefined". I couldn't find any other way to get the index correctly.

like image 876
Usr Avatar asked Jun 18 '17 16:06

Usr


1 Answers

$index will only work for AngularJS, in Angular2 and above the way to get the clicked item index is the following :

<ion-list>
 <ion-item *ngFor="let poi of poiList; let i= index" (click)="openPage(poi, i)">
 <h2> {{ poi.name }} </h2>
</ion-item>
</ion-list>
like image 146
Sarantis Tofas Avatar answered Oct 24 '22 10:10

Sarantis Tofas