Praveen M P
Praveen M P

Reputation: 11614

Get index value based on condition in angular 2 template

I have a json object(items) and I am iterating through ngFor. I need to assign index value to variable i only if it matches the condition item.isRepetable==true

Is there any way I can assign the index value to variable i in *ngFor on condition matches

<ul *ngFor="let item of items;let i=item.isRepetable?index:0">
           <li>{{item}}</li>
</ul> 

Upvotes: 0

Views: 903

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657691

Create and apply a filter pipe, then you don't need a condition and the index will match.

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

There is no way to modify the index generated by *ngFor

Upvotes: 1

Related Questions