Angular dynamically set ng-messages to name attribute

前端 未结 1 905
無奈伤痛
無奈伤痛 2020-12-30 21:12

I dynamically create inputs and want also validate every one of them but cant set correctly the ng-messages attribute to the field name property which is dynamically generat

相关标签:
1条回答
  • 2020-12-30 21:51

    Accessing the properties of your form object can also be done using brackets, which should solve your problem :

    <input ng-model="sub.name" name="subName{{$index}}" class="form-control" placeholder="name" required maxlength="20" />
    <div class="field-error" ng-messages="form['subName' + $index].$error" ng-show="form.Name.$touched" role="alert">
        <div ng-message="required">Name is required.</div>
    </div>
    
    0 讨论(0)
提交回复
热议问题