简体   繁体   English

AngularJs用于更改数组的ng-repeat内的表单的动态名称

[英]AngularJs dynamic name for a form inside ng-repeat for changing array

I'm using the code suggested in answer as follows 我正在使用答案中建议的代码如下

<div ng-repeat="item in itemList" ng-init="formName = 'item_details' + $index">
 <form name="{{formName}}" ng-class="validateForm(formName)">
   //few form elements
 </form>
</div>

But my problem is when the array(itemList) values changes form name remains same. 但我的问题是当数组(itemList)值更改时,表单名称保持不变。 I want form name should be updated based on index value. 我希望表单名称应根据索引值进行更新。

Rather than using $index do use some unique property, may be item has its own id property which defines uniqueness of item. 而不是使用$index确实使用一些独特的属性,可能是item有自己的id属性,它定义了item的唯一性。 So ng-init will look like formName = 'item_details' + item.id" 所以ng-init看起来像formName = 'item_details' + item.id"

<div ng-repeat="item in itemList" ng-init="formName = 'item_details' + item.id">
 <form name="{{formName}}" ng-class="validateForm(formName)">
   //few form elements
 </form>
</div>

Even I'm wondering what is need behind you are making each form with unique name? 即使我想知道你背后需要什么才能使每个表格都有独特的名字? You can have nested forms and innerForm will get validated lonely. 你可以拥有嵌套的表单,而innerForm也会得到验证。 Thereafter the way validation will handle is, when all innerForm gets valid, outForm is valid. 此后验证将处理的方式是,当所有innerForm都有效时, outForm有效。 When any of the innerForm is invalid outerForm will be invalid in that case. 当任何innerForm无效时,在这种情况下, outerForm将无效。

<ng-form ng-repeat="item in itemList" name="outerform">
 <form name="innerForm" ng-class="validateForm(innerForm)">
   //few form elements
 </form>
</div>

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

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