angular - template - ngfortrackby




Get index in a for…of directive in Angular 2 (3)

update, use let, not #

<div *ngFor="let user of users; let i=index">
    {{i}}
</div>

As of now, is there a way to get the current index of the iterable in a for..of directive in Angular 2? In others words, the equivalent of $index in Angular.js v1...

Example of code:

<ul *for="#task of allTasks">
    <li>{{ $index}} - {{ task.label }}</li>
</ul>

(of course this code does not work, it does not provide the current index)


The syntax has been updated and is now (as of Angular2.beta13, April 2016):

<li *ngFor="let item of items; let i = index"></li>

For example:

<li *ngFor="let item of items; let i = index">
   item={{item}} index={{i}}
</li>

<ul>
    <template ngFor let-task [ngForOf]="allTasks" let-i="index">
        <li>{{ i }} - {{ task.label }}</li>
    </template>
</ul>

But you have to use the very most recent version of the quickstart

BTW - the above is the equivalent of the following syntax sugar

<li *ngFor="let task of allTasks; let i=index">{{ i }} - {{ task.label }}</li>




angular