我是angularjs的初学者。我刚开始进行表单验证。一切正常,直到我试图隐藏输入焦点上的错误信息。
当我点击提交按钮时,它会显示错误消息。在我专注于文本框后,它必须隐藏错误消息
这是我的代码:
<form name="form" novalidate>
<div>
<label>Username</label>
<input type="text" name="username" ng-model="username" ng-required="true" ng-minlength="5" ng-maxlength="7"/>
<span class="errors" ng-show="(form.$submitted || form.username.$touched) && form.username.$error.required">Please fill username</span>
<span class="errors" ng-show="form.username.$error.minlength">Username too short</span>
<span class="errors" ng-show="form.username.$error.maxlength">Username too long</span>
</div>
</form>
请有人帮助我
答案 0 :(得分:3)
这只是一个技巧,试试这是否有效:
<form name="form" novalidate>
<div>
<label>Username</label>
<input type="text" name="username" ng-model="username" ng-required="true" ng-minlength="5" ng-maxlength="7" ng-focus="focused=true"/>
<span class="errors" ng-show="(form.$submitted || form.username.$touched) && form.username.$error.required && !focused">Please fill username</span>
<span class="errors" ng-show="form.username.$error.minlength && !focused">Username too short</span>
<span class="errors" ng-show="form.username.$error.maxlength && !focused">Username too long</span>
</div>
</form>
注意:点击提交后,make focused = false;
答案 1 :(得分:0)
我猜你忘了提及ng-app
指令
答案 2 :(得分:0)
使用ng blur在刚离开输入字段时显示即时消息:
<form name="form" novalidate>
<div>
<label>Username</label>
<input type="text" name="username" ng-model="username" ng-required ng-minlength="5" ng-maxlength="7" ng-blur="focused=false" ng-focus="focused=true"/>
<span class="errors" ng-show="(form.$submitted || form.username.$touched) && form.username.$error.required && !focused">Please fill username</span>
<span class="errors" ng-show="form.username.$error.minlength && !focused">Username too short</span>
<span class="errors" ng-show="form.username.$error.maxlength && !focused">Username too long</span>
</div>
</form>