javascript - 如何从使用 ngFor 指令动态添加的 HTML 表中恢复标题单元格数据

标签 javascript angular

我正在尝试从我的 typescript .ts 文件中的数组标题中检索单元格数据。知道这些<th>使用 ngFor 动态生成单元格指示。我只得到用 hard (action, a, b, c, d) 写的列名,而不是从我的 API 动态生成的其他名称。我想检索动态生成的列名。

这是我的 HTML 文件:

<table class="table table-striped">
  <thead id="thead">
    <tr id="head">
      <th class="columnName">Action</th>
      <th class="columnName">a</th>
      <th class="columnName">b</th>
      <th class="columnName">c</th>
      <th class="columnName">d</th>
      <th scope="col" *ngFor="let column of table.columns" class="columnName">{{ column.name }}</th>
    </tr>
  </thead>
</table>

这是我的 component.ts 文件

getColumnName() {
    const theadRow = document.getElementsByClassName('columnName');
    // const thList = theadRow.children;
    console.log(theadRow);

    for (let i = 0; i < 23; i++) {
      console.log(theadRow[i].innerHTML);
    }

这是我的结果: enter image description here

最佳答案

您可以调用 var names = table.columns.map(col => col.name) 来提取名称。

这里有关于 Array.map() 的更多信息

关于javascript - 如何从使用 ngFor 指令动态添加的 HTML 表中恢复标题单元格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57627949/

相关文章:

Angular 2 组件因添加空服务而中断

javascript - 通过figure id访问figcaption和img

javascript - 求和 jQuery 中的文本框列表

javascript - 如何将配置值从 Asp.Net Core MVC 2.0 配置文件推送到 React TypeScript 客户端脚本?

javascript - 我使用 ng-class 有什么问题?

Angular 6 - 路线改变时保持滚动位置

javascript - 在 Chrome 中重新显示 html 输入范围拇指

angular - 2.0.0-beta.7 版 Angular 2 文档的 Material 设计

angular - 在 Angular 5 中验证后防止提交表单

javascript - 使用 RxJS 从订阅中返回一个 Observable