I am trying to make a validation with ng-messages in AngularJs 1.6.4.
我正在尝试用AngularJs 1.6.4中的ng消息进行验证。
But as soon as I type in in the form it shows all 3 ng-messages, and after that they won't go away until a page refresh. So even when the maxlength isn't reached it shows "your name is required".
但是,只要我输入表单,它就会显示所有3条ng-message,之后它们就不会消失,直到页面刷新。因此,即使没有到达maxlength,也会显示“您的名字是必需的”。
<form name="userForm" ng-submit="signup(userForm.$valid)" novalidate>
<!-- First name -->
<div class="form-group" ng-class="{ 'has-error' : userForm.first_name.$touched && userForm.first_name.$invalid }">
<label for="example-text-input" class="col-2 col-form-label">First Name</label>
<div class="col-10">
<input class="form-control" type="text" placeholder="Please enter your first name"
name="first_name"
ng-model="user.first_name"
ng-minlength="2"
ng-maxlength="25"
required>
<div class="help-block" ng-messages="userForm.first_name.$error" ng-if="userForm.first_name.$touched">
<p ng-message="minlength">Your name is too short.</p>
<p ng-message="maxlength">Your name is too long.</p>
<p ng-message="required">Your name is required.</p>
</div>
</div>
</div>
</form>
EDIT
编辑
Solved it by adding the ngMessages to my modules and adding the script tag in my index.html
通过向我的模块添加ngmessage并在index.html中添加脚本标记来解决这个问题
2 个解决方案
#1
2
Your code work for me perfectly. check plunker link
你的代码非常适合我。检查砰砰作响的链接
Injecte ngmessage
in your desired module.
在所需的模块中注入ngmessage。
angular.module('app', ['ngMessages'])
add angular message file in your project.
在项目中添加角消息文件。
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-messages.js"></script>
#2
0
<input class="form-control" type="text" placeholder="Please enter your first name"
name="first_name"
ng-model="user.first_name"
minlength="2"
maxlength="25"
ng-required="true"/>
<div ng-messages="userForm.first_name.$error" ng-show="userForm.first_name.$touched" >
<span ng-message="required" class="error">Required</span>
<span ng-message="minlength">Minimum 2</span>
Maximum 25
最大的25
</div>
#1
2
Your code work for me perfectly. check plunker link
你的代码非常适合我。检查砰砰作响的链接
Injecte ngmessage
in your desired module.
在所需的模块中注入ngmessage。
angular.module('app', ['ngMessages'])
add angular message file in your project.
在项目中添加角消息文件。
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-messages.js"></script>
#2
0
<input class="form-control" type="text" placeholder="Please enter your first name"
name="first_name"
ng-model="user.first_name"
minlength="2"
maxlength="25"
ng-required="true"/>
<div ng-messages="userForm.first_name.$error" ng-show="userForm.first_name.$touched" >
<span ng-message="required" class="error">Required</span>
<span ng-message="minlength">Minimum 2</span>
Maximum 25
最大的25
</div>