简体   繁体   中英

Angular how to find which element in QueryList<ElementRef> is clicked

I have ElmentRef(QueryList) of set of Table cells (td html elements-dynamically created) using ViewChildren. I debugged and have the element set available.

When I click on a particular td html element I call a function and in that function I need to find which element in ElementRef(QueryList) is clicked. How can I do this?

component.html

<table>

    <tr *ngFor="let i of Arr">
      <ng-container *ngFor="let j of Arr">
      
      <td  #tdID (click)="cellClicked(tdID)">
       
      </td>
      </ng-container>
    
    </tr>
    </table>

component.ts

Arr =[1,2,3];
 @ViewChildren('tdID') divs:QueryList<ElementRef>;

cellClicked(cell) {
       console.log("Cell clicked"+cell);
//Help find here which element in the divs QueryList matches "cell"     

}
<table>
    <tr *ngFor="let i of Arr">
      <ng-container *ngFor="let j of Arr">

      <td  #tdID (click)="cellClicked(tdID, i, j)">

      </td>
      </ng-container>

    </tr>
</table>

In you .ts file

cellClicked(tdID, i, j) {
   console.log(tdID, i, j)
}

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