2017-04-17 21 views
2

angular2を使用すると、クリック時に現在の*ngForのインデックス値を取得できますか? クライアントのリストがあり、インデックス値を取得したいと考えています。クリックで現在のインデックスを取得する方法angular2

<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

答えて

4

ngFor指定する

indexは、各テンプレートコンテキストの現在のループ反復に設定されます。

ので、あなたがする必要があります。

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

私は解決策を把握しています。 let i = indexにして、iを関数内のパラメータとして設定します。

<button ion-item *ngFor="let customer of customers; let i = index" (click)="showRadio(i)"> 

クレジット:youtube

0

あなたは顧客オブジェクトのインデックスを持っているし、その関数に

[{"customer_index":1,"customer_name":"jason"}, 
{"customer_index":2,"customer_name":"mary"}]; 

を渡し、その後、機能に渡すことができ

<button ion-item *ngFor="let customer of customers" (click)="showRadio(customer.customer_index)"> 
      {{ customer.customer_name }} 
    </button> 
関連する問題