问题
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