Ionic 3 gets the index of the clicked item
I need to get the index of the clicked item in the ion list, so I can access the position in the array.
The html code I use is:
<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 printed the index in the console, but it shows up as "undefined". I couldn't find any other way to get the index correctly.
+3
User999
source
to share
1 answer
$index
will only work for Angular 1, in Angular 2 and above, to get the index of the clicked element:
<ion-list>
<ion-item *ngFor="let poi of poiList; let i= index" (click)="openPage(poi, i)">
<h2> {{ poi.name }} </h2>
</ion-item>
</ion-list>
+5
Sarantis Tofas
source
to share