简体   繁体   English

如何在键入时删除Angular ng-invalid?

[英]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: 在以下情况下如何停止此操作:

  1. When I haven't typed anything yet 当我还没有输入任何东西时
  2. When I'm still typing 当我还在打字时

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'}">

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM