在* ngFor中输入的内容无法平滑输入Angular 2 [英] Input in *ngFor can not type smoothly Angular 2
本文介绍了在* ngFor中输入的内容无法平滑输入Angular 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的链接是示例代码.
Below link is a sample code.
https://plnkr.co/edit/xmWMm0yjFemdXQzZpJad?p=preview
我有一个对象(测试),其中包含一个数字数组(test.arr).
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,为什么使用 test.arr [i]
而不是v的原因,请参见 "
BTW, the reason why using test.arr[i]
rather than v, see "Cannot assign to a reference or variable!"
推荐答案
如果对原始值 number
, string
使用 * ngFor
,则需要注意Angular能够跟踪身份
If you use *ngFor
with primitive values number
, string
, ... you need to take care Angular is able to track identity.
您可以使用自定义的 trackBy
函数,例如:
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;
}
这篇关于在* ngFor中输入的内容无法平滑输入Angular 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文