代码之家  ›  专栏  ›  技术社区  ›  javapedia.net

角度如何在QueryList<ElementRef>中找到单击的元素

  •  0
  • javapedia.net  · 技术社区  · 6 年前

    帮我理解一下。我有一组使用ViewChildren的表格单元格(动态创建的td html元素)的ElmentRef(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"     
    
    }
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   George C.    6 年前
    <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)
    }