我有 ngModel,在 ngFor 循环内:
<div *ngFor="let comment of comments">
<div *ngFor="let answer of toArray(comment.answers); let j = index; trackBy: trackByIndex;">
<textarea id="editAnswer[j]" name="editAnswer[j]" [(ngModel)]="answer.text">
{{ answer.text }}
</div>
</div>
在我的组件中,我有两个用于索引迭代和将对象转换为数组的函数:
trackByIndex(index: number, obj: any): any {
return index;
}
toArray(answers: object) {
return Object.keys(answers).map(key => ({
key,
...answers[key]
}))
}
但是当我更改 textarea 中绑定的文本时,ngModel 不会改变。
Stackblitz 示例: https ://stackblitz.com/edit/angular-z6xatv