Я пытаюсь подсчитать количество строк в моей таблице, используя директиву ouside 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>
// конечное значение индекса вы можете добиться этого, используя приведенный ниже код
Last value: {{methodResponse.length - 1}}