Jonas
Jonas

Reputation: 7915

Accessing Item index inside ListView item

I'm using a NativeScript list, and I wan't to display the item index inside a list item. But, I can't find a way to achieve this. My code looks like this:

        <StackLayout>
            <Label class="h2 card-title" text="Exercise"></Label>
            <ListView class="list-group" seperatorHeight="0" separatorColor="black"
                [items]="statisticService.exercises" [nsRouterLink]="['../exercise', 3]" itemTemplateSelector="index">
                <ng-template let-item="item">
                    <Label [text]="item.name" class="h3 my-list-group-item"></Label>
                </ng-template>
            </ListView>
        </StackLayout>

With regular Angular I could just use *ngFor="let exercise of statisticService.exercises; let i = index" instead and then do {{i}}, but that doesn't seem to work with nativescript's listview.

Upvotes: 0

Views: 509

Answers (1)

Gilsdav
Gilsdav

Reputation: 1155

Use let-i="index" on your ng-template like described in the doc :

https://docs.nativescript.org/angular/ui/ng-ui-widgets/listview

Upvotes: 1

Related Questions