input loses focus when editing value. Using ngFor

2020-07-23 06:10发布

I have a simple list that binds two-way to a parameter hero in hero.component.ts.

Though when I start typing in the input field it loses focus and I have to click it again. What am I supposed to do in order to allow the user edit the input value without the input field loses target?

 <li *ngFor="let power of hero.powers; let i = index">
      <input [(ngModel)]="hero.powers[i]"/>
 </li>

2条回答
手持菜刀,她持情操
2楼-- · 2020-07-23 06:18

Did you try using trackBy:trackByFn in your ngFor, by using it prevent angular to recreate your DOM and it will keep track of changes

see this tutorial here

查看更多
▲ chillily
3楼-- · 2020-07-23 06:42

Using trackBy function of angular fixes the issue.

live example showing it

查看更多
登录 后发表回答