[英]How to remove Angular ng-invalid while typing?
The error effect (ng-invalid) shows as soon as you select a field. 选择字段后,就会立即显示错误效果(ng-invalid)。 How can I stop this when: 在以下情况下如何停止此操作:
Solution to either one would be great. 解决任何一个问题都很棒。
Use the ng-touched
class in addition to ng-invalid
除了ng-invalid
外,还要使用ng-touched
类
See the documentation: https://docs.angularjs.org/guide/forms 请参阅文档: https : //docs.angularjs.org/guide/forms
input.one.ng-invalid { border: 2px solid red; }
input.two.ng-invalid.ng-touched { border: 2px solid red; }
var app = angular.module('snipApp', []); app.controller('SampleCtrl',['$scope',function($scope){ $scope.inputs = {} }]);
input.one.ng-invalid { border: 2px solid red; } input.two.ng-invalid.ng-touched { border: 2px solid red; }
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script> <div ng-app="snipApp"> <div ng-controller="SampleCtrl"> <form name="myform"> <div>without touched: <input type="text" name="one" ng-model="inputs.one" ng-minlength="4" class="one"> </div> <div>with touched: <input type="text" ng-model="inputs.two" ng-minlength="4" class="two"> </div> <div><pre>{{inputs|json}}</pre></div> </form> </div> </div>
You may want to use ng-model-options
settings' updateOn
(what event should the model value be updated) and allowInvalid
(only if you still need model value to be set with invalid value). 您可能要使用ng-model-options
设置的updateOn
(应该更新模型值的事件)和allowInvalid
(仅当您仍然需要将模型值设置为无效值时)。 Set updateOn
to blur
. 将updateOn
设置为blur
。 Remember you can set the ng-model-options at the top level if you dont want to repeat for every element. 请记住,如果您不想对每个元素重复,可以在顶层设置ng-model-options。
Example:- 例:-
<input type="number" name="num2" ng-model="num2" ng-model-options="{'updateOn':'blur'}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.