如何在Angular中获取Table(array)索引到组件

时间:2018-07-04 13:31:03

标签: html node.js angular typescript

我制作了2个昏暗的数组以在html页面上实现我的数据,

<tbody>
   <tr *ngFor="let i of myarray2">
     <td  *ngFor="let j of i"> {{j}} 
     </td>
   </tr>
</tbody>

它看起来像这样:

enter image description here

我需要一个函数,例如单击加号时,我要将数组(i和j)的索引值传递到组件(.ts文件)中。

2 个答案:

答案 0 :(得分:0)

index设置一些其他变量,并在您的(单击)处理程序中使用它们:

<tbody>
    <tr *ngFor="let subarray of myarray2; let i = index;">
        <td *ngFor="let item of subarray; let j = index;">
          {{item}}
          <span tabindex="0" (click)="someMethod(i, j)">+</button>
        </td>
    </tr>
</tbody>

答案 1 :(得分:0)

在注释的帮助下,我设法获得了参数。这个也可以。非常感谢

html

 <tbody>
                            <tr *ngFor="let i of myarray2 ; let indx1 = index" [attr.data-index]="indx1">
                            <td  *ngFor="let j of i ; let indx2 = index" [attr.data-index]="indx2"  > {{j}} 
                            <button *ngIf="j=='0.0 h'" (click)="getIndexofTable(indx1, indx2)" class="fa fa-plus" > </button>
                             </td>

                        </tr>

打字稿

getIndexofTable(index_value:number,index_value2:number){ }