简体   繁体   中英

Is it possible to increment a component property value inside *ngFor?

I would like to increment a class property inside ngFor for each iteration by 1.Let me know if there is a way.Thanks in advance.

Component Class:

class AA
   {      
  property:number = 0;
   }

Template:

 <div *ngFor = "let sample of samples">
    //increment property here
 </div>

You can use index

 <div *ngFor = "let sample of samples;let i = index">
    <span [class]="'opacity-'+(i+1)">value {{i+1}}</span>
    <button (click)="property = (i+1)">Set Selected Item</button>
 </div>
Selected Item : {{property}}

Just create a function in your class and call it into the ngfor bloc

 class AA { property:number = 0; function increment_property(): void{ this.property++; } }

 <div *ngFor = "let sample of samples"> {{ increment_property() }} </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM