使用angular2可以在点击时获得当前的*ngFor索引值吗?我有一个客户列表,我想要获取它们的索引值。
<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
    });
  }

发布于 2017-04-18 05:04:14
ngFor指定
index将被设置为每个模板上下文的当前循环小版本。
所以你需要做的是:
 <button ion-item *ngFor="let customer of customers; let i = index;" (click)="showRadio()">
          {{ i }} - {{ customer.customer_name }}
 </button>发布于 2017-04-18 04:58:15
我已经想出了解决办法。只需让let i = index和设置i作为函数内部的参数即可。
<button ion-item *ngFor="let customer of customers; let i = index" (click)="showRadio(i)">图片来源:youtube
发布于 2017-04-18 05:00:20
您可以将索引放在customer对象中,然后将其传递给函数
[{"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>https://stackoverflow.com/questions/43459344
复制相似问题