如何通过ng-messages指令获取元素inputName?

时间:2015-04-07 10:52:54

标签: angularjs

如何在不知道输入名称的情况下触发错误消息?请参阅以下代码:

<input class="form-control"
            id="{{field.field_id}}"
            set-name="{{field.field_id}}"
            type="text"
            ng-model="field.field_value"
            ng-minlength="field.field_char_num_min"
            ng-maxlength="field.field_char_num_max"/>

    <div ng-messages="don't know the input name yet" class="my-messages">
        <div ng-message="minlength">Your field is too short</div>
        <div ng-message="maxlength">Your field is too long</div>
    </div>

ng-messages中应该写出输入错误的路径,例如myForm.inputName.$error。但输入将自动生成并附加,因此我无法写入名称,因为生成器设置了名称。我必须得到一个输入名称。我该怎么办?

2 个答案:

答案 0 :(得分:0)

在ng-message

中尝试这样的事情
<form name="formName">
<input type="text" id="anyName"  name="anyName"  required/>
  <span ng-show="formName.anyName.$error.required"> Please enter a value.</span>                            
</form>

答案 1 :(得分:0)

您可以使用field[field.field_id]['$invalid']来确定字段是否有效,然后使用field[field.field_id]['$error'] show minlength&amp; maxlength错误

<强>标记

<div ng-show="field[field.field_id]['$invalid']" class="my-messages">
    <div ng-message="field[field.field_id]['$error'].minlength">Your field is too short</div>
    <div ng-message="field[field.field_id]['$error'].maxlength">Your field is too long</div>
</div>