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.

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply. I initialize form name like name="innerForm{{$index}}" and that worked, even when I delete form name got changes accoedingly.
@Kartik no, it shouldn't be $index it would be the same on removal of item, did you tried what I suggested?
@Kartik does my answer helped?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.