簡體   English   中英

Angular 如何在QueryList中找到哪個元素<ElementRef>被點擊

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

我有一組使用 ViewChildren 的表格單元格(td html 元素-動態創建)的 ElmentRef(QueryList)。 我調試並有可用的元素集。

當我單擊特定的 td html 元素時,我調用了一個函數,在該函數中,我需要找到 ElementRef(QueryList) 中的哪個元素被單擊。 我怎樣才能做到這一點?

組件.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>

組件.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>

在你的 .ts 文件中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM