简体   繁体   English

带双向数据绑定Angular2的嵌套ngFor

[英]Nested ngFor with two-way data binding Angular2

I try to make a table of input fields with help of nested ngFor with two-way data binding. 我尝试在嵌套ngFor和双向数据绑定的帮助下制作一个输入字段表。 I almost achieved this, but something with two-way data binding is wrong and I can't find a problem. 我几乎实现了这一点,但是使用双向数据绑定的某些操作是错误的,我找不到问题。

So, this is what I want to have - I want to be able type each letter of word in. 所以,这就是我想要的-我希望能够输入每个单词的字母。

我的输入示例的表格

I have this table, and I am able to type in, but if I type in first cell, the same value appears in second, if I type in third, value appears also in 4th and 5th... And I can't understand why, I guess the problem is with this line 我有此表,可以键入,但是如果我在第一个单元格中键入,则在第二个中出现相同的值,如果我在第三个单元中键入,则值也出现在第4位和第5位...而且我无法理解为什么,我想问题在于这条线

<input maxlength="1" size='5' name="{{i}}{{j}}" [(ngModel)]="helper[i][j]" [id]="j"/>

Could you please tell me, how could I fix this one? 你能告诉我,我该怎么解决? Here is my plunker, it works, but with this mistake, that I described above... My plunker is here 这是我的插件,它可以工作,但是由于上面描述的这个错误, 我的插件在这里

When using *ngFor , you should try to avoid accessing directly the arrays you are iterating on, like with [(ngModel)]="helper[i][j]" . 使用*ngFor ,应避免直接访问要迭代的数组,例如[(ngModel)]="helper[i][j]"

Generally, you should try to always works with full object and dot(.) notation when using data binding. 通常,在使用数据绑定时,应尝试始终使用完整的对象和dot(。)表示法。

In your case, just replace the raw array ['','','','',''] by an array of objects [value: '', value: '', value: '', value: '', value: ''] and bind it directly. 在您的情况下,只需将原始数组['','','','','']替换为对象数组[value: '', value: '', value: '', value: '', value: '']并将其直接绑定。

<td *ngFor="let item2 of helper[i]; let j = index;">
    <input maxlength="1" size='5' name="{{i}}{{j}}" [(ngModel)]="item2.value" [id]="j"/>
</td>

I have updated your plunker 我已经更新了你的矮人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM