简体   繁体   English

Angular 会在“必填”字段上自动添加“ng-invalid”类

[英]Angular is automatically adding 'ng-invalid' class on 'required' fields

I am building an angular app for which I have some forms set up.我正在构建一个角度应用程序,我为其设置了一些表单。 I have some fields that are required to be filled before submission.我有一些字段需要在提交前填写。 Therefore I have added 'required' on them:因此,我在它们上添加了“必需”:

<input type="text" class="form-control" placeholder="Test" ng-model="data.test" required>

However when I launch my app, the fields are displayed as 'invalid' and the classes 'ng-invalid' and 'ng-invalid-required' even before the submit button has been click or before the user has typed anything in the fields.但是,当我启动我的应用程序时,即使在单击提交按钮或用户在字段中输入任何内容之前,字段也显示为“无效”和类“ng-invalid”和“ng-invalid-required”。

How can I make sure that thoses 2 classes are not added immediately but either once the user has submitted the form or when he has typed something wrong in the corresponding field?如何确保不会立即添加那些 2 个类,而是在用户提交表单或在相应字段中输入错误时添加?

Since the inputs are empty and therefore invalid when instantiated, Angular correctly adds the ng-invalid class.由于输入是空的,因此在实例化时无效,Angular 正确添加了ng-invalid类。

A CSS rule you might try:您可以尝试的 CSS 规则:

input.ng-dirty.ng-invalid {
  color: red
}

Which basically states when the field has had something entered into it at some point since the page loaded and wasn't reset to pristine by $scope.formName.setPristine(true) and something wasn't yet entered and it's invalid then the text turns red .这基本上说明了自从页面加载以来该字段在某个时间点输入了一些内容并且没有被$scope.formName.setPristine(true)重置为原始状态并且尚未输入某些内容并且它无效然后文本变为red

Other useful classes for Angular forms (see input for future reference ) Angular 表单的其他有用类(请参阅输入以供将来参考

ng-valid-maxlength - when ng-maxlength passes ng-valid-maxlength - 当ng-maxlength通过时
ng-valid-minlength - when ng-minlength passes ng-valid-minlength - 当ng-minlength通过时
ng-valid-pattern - when ng-pattern passes ng-valid-pattern - 当ng-pattern通过时
ng-dirty - when the form has had something entered since the form loaded ng-dirty - 自从表单加载以来表单已经输入了一些东西
ng-pristine - when the form input has had nothing inserted since loaded (or it was reset via setPristine(true) on the form) ng-pristine - 当表单输入自加载后没有插入任何内容时(或者通过setPristine(true)上的setPristine(true)重置)
ng-invalid - when any validation fails ( required , minlength , custom ones, etc) ng-invalid - 当任何验证失败时( requiredminlength 、 custom 等)

Likewise there is also ng-invalid-<name> for all these patterns and any custom ones created.同样,所有这些模式和创建的任何自定义模式也有ng-invalid-<name>

Thanks to this post, I use this style to remove the red border that appears automatically with bootstrap when a required field is displayed, but user didn't have a chance to input anything already:感谢这篇文章,我使用这种样式删除了显示必填字段时随引导程序自动出现的红色边框,但用户还没有机会输入任何内容:

input.ng-pristine.ng-invalid {
    -webkit-box-shadow: none;
    -ms-box-shadow: none;
    box-shadow:none;
}

Since the fields are empty they are not valid, so the ng-invalid and ng-invalid-required classes are added properly.由于字段为空,它们无效,因此正确添加了ng-invalidng-invalid-required类。

You can use the class ng-pristine to check out whether the fields have already been used or not.您可以使用类ng-pristine来检查字段是否已被使用。

Try to add the class for validation dynamically, when the form has been submitted or the field is invalid.当表单已提交或字段无效时,尝试动态添加用于验证的类。 Use the form name and add the 'name' attribute to the input.使用表单名称并将“名称”属性添加到输入中。 Example with Bootstrap: Bootstrap 示例:

<div class="form-group" ng-class="{'has-error': myForm.$submitted && (myForm.username.$invalid && !myForm.username.$pristine)}">
    <label class="col-sm-2 control-label" for="username">Username*</label>
    <div class="col-sm-10 col-md-9">
        <input ng-model="data.username" id="username" name="username" type="text" class="form-control input-md" required>
    </div>
</div>

It is also important, that your form has the ng-submit="" attribute:同样重要的是,您的表单具有 ng-submit="" 属性:

<form name="myForm" ng-submit="checkSubmit()" novalidate>
 <!-- input fields here -->
 ....

  <button type="submit">Submit</button>
</form>

You can also add an optional function for validation to the form:您还可以向表单添加用于验证的可选函数:

//within your controller (some extras...)

$scope.checkSubmit = function () {

   if ($scope.myForm.$valid) {
        alert('All good...'); //next step!

   }
   else {
        alert('Not all fields valid! Do something...');
    }

 }

Now, when you load your app the class 'has-error' will only be added when the form is submitted or the field has been touched.现在,当您加载应用程序时,只有在提交表单或触摸字段时才会添加类“has-error”。 Instead of:代替:
!myForm.username.$pristine !myForm.username.$pristine

You could also use:您还可以使用:
myForm.username.$dirty myForm.username.$dirty

the accepted answer is correct.. for mobile you can also use this (ng-touched rather ng-dirty)接受的答案是正确的..对于移动设备,您也可以使用它(ng-touched 而不是 ng-dirty)

input.ng-invalid.ng-touched{
  border-bottom: 1px solid #e74c3c !important; 
}

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

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