小编jak*_*ard的帖子

如何获取使用 ngfor 循环创建的 html 元素

大家好,这里是我的问题的简化

我使用 *ngfor 在模板中创建了元素

<table >
  <thead>
  <tr>
    <th *ngFor="let item of array" > {{item}}</th>
  </tr>
  </thead>

 </table> 
Run Code Online (Sandbox Code Playgroud)

在 .ts 文件中我有表格

array = [1,2,3,4];
Run Code Online (Sandbox Code Playgroud)

然后当我尝试在控制台中显示元素时

  var cells = document.getElementsByTagName('th'); //or getElementById or anything..
  console.log(cells[1]);

Run Code Online (Sandbox Code Playgroud)

它显示 未定义

ngfor angular

5
推荐指数
1
解决办法
2190
查看次数

标签 统计

angular ×1

ngfor ×1