[英]Values are duplicated into all input field which is created using the ng-repeat and ng-model
I am using the ng-repeat
to create the input fields dynamically, everything seems to be working fine and the input fields are created but due to some reason when I enter some value into that created input field then due to two-way data binding the value is entered simultaneously in all the text fields that were created.我正在使用
ng-repeat
动态创建输入字段,一切似乎都工作正常并且输入字段已创建但由于某种原因,当我在创建的输入字段中输入一些值时,由于双向数据绑定在创建的所有文本字段中同时输入值。
As per my knowledge, I am assigning the different ng-model to each of the created fields so I am not sure why the values are being copied.据我所知,我为每个创建的字段分配了不同的 ng-model,所以我不确定为什么要复制这些值。 When I try to print the values which are assigned to the input field just above the field then I see the id is different wanted to know why all the fields are taking the same value and also how can I prevent it.
当我尝试打印分配给字段上方输入字段的值时,我看到 id 不同,想知道为什么所有字段都采用相同的值以及如何防止它。
HTML Code
: HTML Code
:
<tr>
<td ng-repeat="extension in ExtensionList">
<!-- Here the value displayed are different -->
{{ formdata.extension.ExtensionVlaues }}
<input type="text" class="form-control" id="extension.ExtensionVlaues" ng-model="formdata.extension.ExtensionVlaues">
</td>
</tr>
Here is the screenshot from the front-end:这是前端的屏幕截图:
Here is the example of my ExtensionList
: {"Field1":"","Field2":"","Field3":1,"ExtensionVlaues":2}
这是我的
ExtensionList
的示例: {"Field1":"","Field2":"","Field3":1,"ExtensionVlaues":2}
As per my knowledge, the given ng-model
is different but still not working.据我所知,给定的
ng-model
是不同的,但仍然无法正常工作。 Can anyone please help me with this?谁能帮我解决这个问题? I tried few things like
ng-model="formdata.extension[ExtensionVlaues]"
but still no luck.我尝试了一些类似
ng-model="formdata.extension[ExtensionVlaues]"
东西,但仍然没有运气。
I tried to assign ng-model like this but it is not working:我试图像这样分配 ng-model 但它不起作用:
ng-model="formdata.extension[ExtensionVlaues]"
Based on the below answer I tried 2 different things it but it's not working and getting the same issue.根据以下答案,我尝试了 2 种不同的方法,但它不起作用并且遇到相同的问题。 Please let me know how can I resolve this:
请让我知道如何解决此问题:
<td ng-repeat="(key, extension) in ExtensionList">
<input type="text" ng-model="formdata.extension.key">
</td>
<td ng-repeat="(key, extension) in ExtensionList">
<input type="text" ng-model="formdata.extension.ExtensionVlaues">
</td>
You are trying to use ExtensionList as an array.您正在尝试将 ExtensionList 用作数组。 You need to use it as an object with ng-repeat:
您需要将其用作带有 ng-repeat 的 object:
<td ng-repeat="(key, extension) in ExtensionList">
<input type="text" ng-model="formdata[extension][key]">
</td>
<td ng-repeat="(key, extension) in ExtensionList">
<input type="text" ng-model="formdata[extension][ExtensionVlaues]">
</td>
Just incase anyone else also struck at this issue:以防万一其他人也遇到了这个问题:
<span ng-repeat="(key, extension) in ExtensionList" class="form-inline">
<br/>
<span>
{{ extension.NameSpace + ":" + extension.LocalName }}
</span> 
<input type="text" class="form-control" id="extension.ExtensionVlaues" ng-model="ExtensionList.extension[key].FreeText" ng-blur="ExtensionText(ExtensionList.extension[key].FreeText, extension.ExtensionVlaues)">
</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.