如何获取单击angular2的当前索引

前端之家收集整理的这篇文章主要介绍了如何获取单击angular2的当前索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用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
    });

  }

enter image description here

解决方法

ngFor指定

index将设置为每个模板上下文的当前循环迭代.

所以你需要这样做:

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

猜你在找的Angularjs相关文章