我是angularjs的新手,我只是想知道ngMessage的“ ng-messages”属性可以做什么。

我正在尝试为多个input [type = text]元素显示一个ngMessage。例如:

<input type="text" id="name1" ng-model="person.name1" />
<input type="text" id="name2" ng-model="person.name2" />
<input type="text" id="name3" ng-model="person.name3" />
<input type="text" id="name4" ng-model="person.name4" />

<div id="error"
ng-messages="person.name1.$error ||
             person.name2.$error ||
             person.name3.$error ||
             person.name4.$error"

ng-show="person.name1.$touched ||
         person.name2.$touched ||
         person.name3.$touched ||
         person.name4.$touched"
ng-include="errorMsg.html"
/>


对于所有4个input [type = text]属性,将显示一条验证消息。

最佳答案

尝试以以下方式使用它:-

<form name="Form_Name">
  <label>
    Enter your name:
    <input type="text"
           name="myName"
           ng-model="name"
           ng-minlength="5"
           ng-maxlength="20"
           required />
  </label>
  <pre>myForm.myName.$error = {{ myForm.myName.$error | json }}</pre>

  <div ng-messages="myForm.myName.$error" style="color:maroon" role="alert">
    <div ng-message="required">You did not enter a field</div>
    <div ng-message="minlength">Your field is too short</div>
    <div ng-message="maxlength">Your field is too long</div>
  </div>
</form>

09-13 01:33