1

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. I want form name should be updated based on index value.

Community
  • 1
  • 1
Kartik
  • 71
  • 1
  • 2
  • 11

1 Answers1

0

Rather than using $index do use some unique property, may be item has its own id property which defines uniqueness of item. So ng-init will look like 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. Thereafter the way validation will handle is, when all innerForm gets valid, outForm is valid. When any of the innerForm is invalid outerForm will be invalid in that case.

<ng-form ng-repeat="item in itemList" name="outerform">
 <form name="innerForm" ng-class="validateForm(innerForm)">
   //few form elements
 </form>
</div>
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Thanks for reply. I initialize form name like `name="innerForm{{$index}}"` and that worked, even when I delete form name got changes accoedingly. – Kartik Dec 14 '16 at 10:35
  • @Kartik no, it shouldn't be `$index` it would be the same on removal of item, did you tried what I suggested? – Pankaj Parkar Dec 14 '16 at 11:49
  • @Kartik does my answer helped? – Pankaj Parkar Dec 15 '16 at 16:10
  • as per your suggestion I cannot use item.id, as there is no id value associated with it and I cannot change my array object. And yes $index worked bcoz I wanted just the form name as per their index value. If arrat value is modified then form name also modified according to its index value. Thanks – Kartik Dec 16 '16 at 17:07