Angular JS ng-Messages不显示错误消息

时间:2016-08-22 15:17:45

标签: angularjs validation gruntjs bower ng-messages

我是使用Angular JS进行表单验证的新手,所以我环顾了很多,发现一个很好的教程。可以找到该教程here。我知道Stack Overflow上有一些关于ng-messages的帖子,但是我对它们进行了审核,但它们似乎与我的问题无关。

我已经使用bower安装了ng-messages,并使用grunt运行它。没有错误,但我的验证消息似乎没有触发。

我的HTML如下:

<form name="myForm" novalidate>
  <fieldset>

    <div id="number" ng-class="{ 'has-error' : myForm.clientNumber.$invalid && !myForm.clientNumber.$pristine }">
      <label for="client">SRF #: *</label>
      <input type="text" name="clientNumber" placeholder="Enter SR #" ng-minlength="3" ng-maxlength="9" required>
      <div ng-messages="myForm.clientNumber.$error">
        <!--<div ng-message="required" >SRF # is required.</div>-->
        <div ng-message="minlength" >SRF Number is too short.</div>
        <!--<div ng-message="maxlength">SRF Number is too long.</div>-->
       </div>
     </div>
   </fieldset>
</form>

我app.JS的一部分内容如下:

angular
  .module('myApp', [
  // Angular
  'ngAnimate',
  'ngCookies',
  'ngResource',
  'ngSanitize',
  'ngTouch',
  'ui.router',
  'myApp.home',
  'restangular',
  'ngMessages'
])

我的控制器的一部分如下:

(function() {
  'use strict';
  angular.module('myApp.home', [
  'ui.router',
  'myApp.myFactory',
  'ngMessages'
])
.config(homeConfig)
.controller('homeCtrl', home);

homeConfig.$inject = ['$stateProvider'];
home.$inject = ['$http','myFactory','$timeout'];

function home($http,myFactory,$timeout) {
  ...
}

我看过并尝试过的内容:

  1. ngMessages/angular validation not working
  2. ng-messages is not working
  3. https://github.com/angular/material/issues/6440
  4. https://github.com/angular/material/issues/2820
  5. https://docs.angularjs.org/api/ngMessages
  6. http://www.yearofmoo.com/2014/05/how-to-use-ngmessages-in-angularjs.html
  7. 列表中的一些是教程,而其他是Stack Overflow上的帖子:ngMessages/angular validation not working

    我不明白为什么它不起作用。我见过的所有网站看起来都是以同样的方式实现的。

2 个答案:

答案 0 :(得分:4)

我们需要做的就是在输入中添加ng-model

  

ngModel指令使用NgModelController将输入,select,textarea(或自定义表单控件)绑定到作用域上的属性,NgModelController由此指令创建并公开。

简而言之,在ng-model元素上没有<input>,Angular并不关心它。

查看下面的工作片段。

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">

  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular-messages.js"></script>

    <script>
      var app = angular.module('app', ['ngMessages']);
      app.controller('RegistrationScreenController',  function() {});
    </script>
    
  </head>

  <body ng-app="app" ng-controller="RegistrationScreenController">
    
    <form name="myForm" novalidate>
      <fieldset>
        <div id="number" ng-class="{ 'has-error' : myForm.clientNumber.$invalid && !myForm.clientNumber.$pristine }">
          <label for="client">SRF #: *</label>
          <input type="text" ng-model="clientNumber" name="clientNumber" placeholder="Enter SR #" ng-minlength="3" ng-maxlength="9" required>
          <div ng-messages="myForm.clientNumber.$error">
            <div ng-message="required" >SRF # is required.</div>
            <div ng-message="minlength" >SRF Number is too short.</div>
            <div ng-message="maxlength">SRF Number is too long.</div>
          </div>
        </div>
      </fieldset>
    </form>
  </body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

# From what i can see, you are missing 'when=required' in your 'ng-message' 
# This should fix it. Refer to examples below.
# 1) Using 'ng-messages'
# 2) Using 'ng-show'

//---------------------------------------------------------------------------------------------------------------
# HTML - Angular Material Design
# Follow the following example below and it should work.
# Am basically using 'ng-message' to show the error message

<form name="editUserForm">
    <div flex layout="row">
        <md-input-container flex="100">
            <label for="firstName">First Name</label>
            <md-icon md-font-icon="zmdi zmdi-account" style="color:#47B2E8"></md-icon>
            <input id="firstNameEditUser" 
                label="firstName" 
                name="firstName" 
                type="text" 
                ng-model="vm.contact.firstName"
                required/>
            <div ng-messages="editUserForm.firstName.$error">
                <div ng-message when="required"><span translate>Please enter your First Name</span></div>
            </div>
        </md-input-container>
    </div>
</form>

//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
# HTML - Angular Material Design
# Follow the following example below and it should work.
# Am basically using 'ng-show' to hide and show the error
# messages

<form name="changePasswordForm">
    <md-input-container class="md-block">
        <label for="password">Password</label>
        <input id="password"
            label="password"
            name="password"
            type="password"
            ng-model="vm.user.password"
            required/>
            <div class="validation-messages-error">
                <div ng-show="changePasswordForm.password.$dirty && changePasswordForm.password.$error.minlength">
                    <span>Password is too short</span>
                </div>
                <div ng-show="changePasswordForm.password.$dirty && changePasswordForm.password.$error.maxlength">
                      <span>Password is too long</span>
                </div>
                <div ng-show="changePasswordForm.password.$error.required">
                      <span>Password required</span>
                </div>
            </div>
    </md-input-container>
</form>


//---------------------------------------------------------------------------------------------------------------
# CSS Class

.validation-messages-error {
  font-size: 12px;
  color: #dd2c00;
  margin: 10px 0 0 25px;
}

//---------------------------------------------------------------------------------------------------------------