Given the following example within a PrimeNG Table, there should be a skeleton visible while loading data:
<ng-template pTemplate="loadingbody">
<tr>
@for (i of [].constructor(3); track $index) {
<td>
<p-skeleton />
</td>
}
</tr>
</ng-template>
In this example, the variable $index
is used as a track. Does this have a disadvantage? Could this not be used by all for loops?
Alternatively, you could also make a member field in the class:
class TableComponent {
protected readonly columnCount = Array.from({length: 3}).map((,index) => index);
}
Would this approach be different from using the $index variable?
3
$index
should only be used for static collections, see the docs. So no, it is not suitable as a default for all loops. If the collection changes at runtime, using $index
can lead to the DOM not reacting to the changes.
In your case however it’s totally fine to use $index
, as the collection never changes.