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.
$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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With