简体   繁体   中英

How to get Table(array)index in to component in Angular

I made 2 dim-array to implement my data on html page,

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

It looks like this:

在此处输入图片说明

I need a function like when I click the plus I want to pass index values of my array(i and j) into my component (.ts file).

Set some additional variables to the index and use them in your (click) handler:

<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>

I managed to get as parameters with help of the comment.This one also works .Thanks a lot

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>

typescript

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM