Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current index on click angular2

Tags:

angular

With angular2 is it possible to get the current *ngFor index value on click? I have a list of clients and I want to get their index value.

<button ion-item *ngFor="let customer of customers" (click)="showRadio()">
    {{ customer.customer_name }}
</button>
  showRadio(currentIndex) {
  //................    

  for(var j=0; j<myResult[currentIndex].bookingIds.length; j++) {
    alert.addInput({
      type: 'radio',
      label: myResult[currentIndex].bookingIds[j],
      value: myResult[currentIndex].bookingIds[j],
      checked: false
    });

  }

enter image description here

like image 529
Santosh Avatar asked Apr 17 '17 20:04

Santosh


1 Answers

ngFor specify

index will be set to the current loop iteration for each template context.

so you need to do:

 <button ion-item *ngFor="let customer of customers; let i = index;" (click)="showRadio()">
          {{ i }} - {{ customer.customer_name }}
 </button>
like image 175
Sam Avatar answered Oct 16 '22 19:10

Sam