ngFor with index as value in attribute in angular 12

Forums AngularngFor with index as value in attribute in angular 12
Staff asked 3 years ago

I have a ngFor loop which keeps track of the current index. I want to store that index value in an attribute.

<ul *ngFor="#item of items; #i = index" data-index="#i">
    <li>{{item}}</li>
</ul>

I want to store the value of #i in the attribute data-index.

Can any one guide me how to do this ?

Thanks.

Answers (1)

Add Answer
Staff answered 3 years ago

Try This one:

<ul *ngFor="#item of items; #i = index"  [attr.data-index]="#i">
    <li>{{item}}</li>
</ul>

 

Subscribe

Select Categories