本文介绍了Angular动态设置ng-messages到name属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态创建输入并希望验证其中的每一个,但是无法将ng-messages属性正确设置为动态生成的字段名称属性。

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 generated.

<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>

我在第二行遇到问题,我将ng-messages动态设置为ng-messages。我该怎么做?

I got problem with second line where i set the ng-messages dynamically to ng-messages. How can I do this?

推荐答案

访问表单对象的属性也可以使用括号来完成,这可以解决你的问题:

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>

这篇关于Angular动态设置ng-messages到name属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:20