2017-04-17 46 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

ng對於指定

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>