Input in *ngFor can not type smoothly Angular 2

谁都会走 提交于 2020-01-05 04:57:48

问题


Below link is a sample code.

https://plnkr.co/edit/xmWMm0yjFemdXQzZpJad?p=preview

I have an object(test), which contain a number array(test.arr).

<div *ngFor="let v of test.arr; let i = index">
    <input [(ngModel)]="test.arr[i]" type="text">
</div>

Once I type something on input, it will missing focus.

BTW, the reason why using test.arr[i] rather than v, see "Cannot assign to a reference or variable!"


回答1:


Plunker example

If you use *ngFor with primitive values number, string, ... you need to take care Angular is able to track identity.

You can use a custom trackBy function like:

<div *ngFor="let v of test.arr; let i = index;trackBy:trackByIdx">
trackByIdx(index, val) {
  return index;
}


来源:https://stackoverflow.com/questions/42200061/input-in-ngfor-can-not-type-smoothly-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!