AngularJs dynamic name for a form inside ng-repeat

前端 未结 2 827
野的像风
野的像风 2020-12-06 09:46

How do I name a form inside ng-repeat with a dynamic name?

相关标签:
2条回答
  • 2020-12-06 10:02

    You can just do:

    <form name="item_details{{$index}}" ng-class="validateForm('item_details'+$index)">
    

    EDIT:

    You can use ng-init as well, like so:

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

    If it's just to apply a class based on validity of the form, then you could apply styling to automatically added classes, such as: ng-valid:

    .ng-valid {
       background-color: green;
    }
    
    0 讨论(0)
  • 2020-12-06 10:04

    You may access the instance of the form by the dynamic name using bracket notation:

    <div ng-repeat="item in itemList">
      <form name="item_details{{$index}}" ng-class="validateForm(this['item_details' + $index])">
        //few form elements
      </form>
    </div>
    
    0 讨论(0)
提交回复
热议问题