我是使用 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'
])
我的部分 Controller 如下:
(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) {
...
}
我看过和尝试过的:
- ngMessages/angular validation not working
- ng-messages is not working
- https://github.com/angular/material/issues/6440
- https://github.com/angular/material/issues/2820
- https://docs.angularjs.org/api/ngMessages
- http://www.yearofmoo.com/2014/05/how-to-use-ngmessages-in-angularjs.html
列表中的一些是教程,而另一些是 Stack Overflow 上的帖子:ngMessages/angular validation not working .
我不明白为什么它不起作用。我见过的所有网站看起来都以相同的方式实现。
最佳答案
我们需要做的就是添加 ng-model
到我们的输入。
The ngModel directive binds an input,select, textarea (or custom form control) to a property on the scope using NgModelController, which is created and exposed by this directive.
简而言之,没有ng-model
在 <input>
element Angular 不关心它。
请看下面的工作片段。
<!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>
关于angularjs - Angular JS ng-Messages 不显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39083213/