简体   繁体   中英

angular2-counto inside *ngFor

I'm trying to use the angular2-counto directive inside an *ngFor loop.
I know that I need a unique string in each instance. So

<div 
    counto
    [step]="30"
    [countTo]="myArr[0].Amount"
    [countFrom]="0"
    [duration]="2"
    (countoChange)="counto_0 = $event"
    (countoEnd)="onCountoEnd()">
      {{counto_0 | number:'1.0-0'}}
  </div>
  <div 
    counto
    [step]="30"
    [countTo]="myArr[1].Amount"
    [countFrom]="0"
    [duration]="2"
    (countoChange)="counto_1 = $event"
    (countoEnd)="onCountoEnd()">
      {{counto_1 | number:'1.0-0'}}
</div>
...

Should become

<div *ngFor="let item of myArr"
    counto
    [step]="30"
    [countTo]="item.Amount"
    [countFrom]="0"
    [duration]="2"
    (countoChange)="??? = $event"
    (countoEnd)="onCountoEnd()">
      {{??? | number:'1.0-0'}}
  </div>

But what can I use in the ??? as a unique param for each iteration of the loop? The counto directive needs a string param (I think). Attempts to use *ngFor=let i item of myArr; let i = index; *ngFor=let i item of myArr; let i = index; and then somehow use the index in the directive don't work.

<div *ngFor="let item of myArr; let i = index;"
    counto
    [step]="30"
    [countTo]="item.Amount"
    [countFrom]="0"
    [duration]="2"
    (countoChange)="i = $event"
    (countoEnd)="onCountoEnd()">
      {{i | number:'1.0-0'}}
  </div>

Results in Uncaught Error: Cannot assign to a reference or variable!

You are trying to assign $event to the i variável. Try something like:

newVar[i] = $event 

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