angular - 在 ngfor 之外使用计数器
问题描述
我正在尝试在 ngFor 指令之外使用它来计算表中的行数,如下代码所示
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">IP</th>
<th scope="col">Session_ID</th>
</tr>
</thead>
<tbody>
<tr *ngFor='let obj of methodResponse; let theIndex = index;'>
<th scope="row"> {{theIndex + 1}}</th>
<td>{{obj.name}}</td>
<td>{{obj.sourceIp}}</td>
<td>{{obj.sessionId}}</td>
</tr>
<!-- I want to use the final value of theIndex value here -->
</tbody>
</table>
如何计算每一行并在 ngFor 之外使用它,我试图在 ngFor 内部调用一个函数,但它对我不起作用,有什么帮助吗?
解决方案
您可以使用length
数组的属性来显示表格中有多少元素。
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">IP</th>
<th scope="col">Session_ID</th>
</tr>
</thead>
<tbody>
<tr *ngFor='let obj of methodResponse; let theIndex = index;'>
<td scope="row"> {{theIndex + 1}}</td>
<td>{{obj.name}}</td>
<td>{{obj.sourceIp}}</td>
<td>{{obj.sessionId}}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td>{{methodResponse.length}}</td>
</tr>
</tfoot>
</table>
推荐阅读
- java - 更正应用程序的类路径,使其包含一个兼容的 javax.annotation.Resource 版本
- css - CSS 媒体查询不适用于确切的 767
- asynchronous - 如何在测试类中覆盖完成方法?
- java - 使用 ant java.lang.ClassNotFoundException 运行 ptest: org.pitest.coverage.execute.CoverageMinion
- javascript - 如何从嵌套树结构中分离单个对象
- flutter - 我需要使用带有动态图像图标和价格的自定义画家制作一张位图图像,如颤动
- install4j - Install4j 项目构建失败
- terraform - Terragrunt - 不支持的属性 --- 但 tfstate 具有该属性
- oracle - Oracle Insert 查询在生产环境中需要很长时间,但在较低环境中执行时间为 3 秒
- javascript - 如何完成删除div?