Nativescript – Angular 4, how to take the selected item's data in itemTap() in listview

Issue

Find the below Listview code, where I have written the onItemTap($event) method in (itemTap).

.html

<ListView [items]="customer" (itemTap)="onItemTap($event)" class="list-group">
            <ng-template let-item="item">
                    <StackLayout class="list-group-item">
                            <Label [text]="item.name"></Label>
                            <Label [text]="item.email"></Label>
                            <Label [text]="item.phoneNumber"></Label>
                    </StackLayout>
            </ng-template>
    </ListView>

.ts

onItemTap(args) { 
    console.log(args.data);
    console.log(args.view);
    console.log(args.index);
    console.log(args.object);
 }

How I take the data of item.name / item.email / item.phonenumber when we do tap.

Solution

You have your answer hidden in your example. You have access to index of the tapped item.

onItemTap(args) {
    console.log(this.customer[args.index].name);
}

Otherwise you can listen to tap events on your item’s template – StackLayout.

Answered By – JakubP

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.