Is there a downside to using $index as track in a for loop

Discussão em 'Angular' iniciado por kerosene, Setembro 26, 2024.

  1. kerosene

    kerosene Guest

    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?

    Continue reading...

Compartilhe esta Página