Angular JS的ng-Messages无法显示错误信息

3
我是一名新手,想使用AngularJS进行表单验证,所以我查找了很多资料,并找到了一个不错的教程。该教程可以在这里找到。我知道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 验证不起作用
  2. ng-messages 不起作用
  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

列表中有些是教程,有些是在 Stack Overflow 上的帖子:ngMessages/angular 验证不起作用

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


你的HTML代码中没有任何事件可以展示ngMessage。请查看这个StackOverflow答案 - gaurav bhavsar
它需要一个ng-submit函数,否则它不会起作用。 - L1ghtk3ira
我把它放进去了,但它不起作用,我需要一个例子。我在它上面尝试了我的ng-click方法,但那个函数不再触发。 - L1ghtk3ira
你检查了plunker吗? - gaurav bhavsar
2个回答

5
我们只需要在输入框中添加ng-model即可。简单来说,如果在<input>元素上没有ng-model,Angular就不会关心它。NgModel指令使用NgModelController将输入框、下拉框、文本域(或自定义表单控件)绑定到作用域上的属性,并创建并公开此指令。请查看下面的代码片段以了解其工作原理。

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


我会尝试,但是为什么我们需要ng-model才能使它工作?您能否在您的答案中添加解释。谢谢。 - L1ghtk3ira
嘿,它可以工作了,谢谢 :) 我不知道它需要这个。请在你的答案中添加任何解释。 - L1ghtk3ira
太棒了!很高兴能帮到你。我会附上解释更新的。 - oKonyk

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接