angularjs |表格验证| ngMessages不显示错误消息

时间:2015-10-03 08:35:30

标签: forms validation ng-messages

我能够得到错误,就像字段变红,显示字符数变红。但错误消息没有显示。在控制台中找不到错误。我还添加了angular-1.3.4 ngMessages依赖项。请帮忙。

<div class="col-md-3 col-sm-6 col-xs-12">
<md-input-container flex>
          <label>{{field.field_title}}</label>
          <input ng-model="field.field_value" name="username" type="text" placeholder="Placeholder text"  
            ng-required="field.field_required" md-maxlength="field.max" >

     <div ng-messages="userForm.username.$error">

        <p ng-message="maxlength">Your name is too long.</p>
        <p ng-message="required">Your name is required.</p>
    </div>
 </md-input-container>
</div>

1 个答案:

答案 0 :(得分:0)

保持输入字段的这些属性简单:

NG-MINLENGTH = 3 NG-MAXLENGTH = 20

这是我的验证工作代码:

<form name="signup_form" novalidate ng-submit="signupForm()">
  <fieldset>
    <legend>Signup</legend>
    <div class="row">
      <div class="large-12 columns">
        <label>Your name</label>
        <input type="text" 
            placeholder="Name" 
            name="name" 
            ng-model="signup.name" 
            ng-minlength=3 
            ng-maxlength=20 required />
       <div class="error-container" 
            ng-show="signup_form.name.$dirty && signup_form.name.$invalid && signup_form.submitted">
        <small class="error" 
            ng-show="signup_form.name.$error.required">
            Your name is required.
        </small>
        <small class="error" 
                ng-show="signup_form.name.$error.minlength">
                Your name is required to be at least 3 characters
        </small>
        <small class="error" 
                ng-show="signup_form.name.$error.maxlength">
                Your name cannot be longer than 20 characters
        </small>
      </div>
      </div>
    </div>
    <button type="submit" class="button radius">Submit</button>
  </fieldset>
</form>