简体   繁体   English

CSS验证规则适用于AngularJS v1.0.8的onload

[英]CSS validation rules applied onload with AngularJS v1.0.8

I have a UI issue with AngularJS, now please bare in mind that I have been working with Angular.js for 2 hours now and I have never used it before. 我在AngularJS中遇到UI问题,现在请记住,我已经使用Angular.js已有2个小时了,而我以前从未使用过它。 I have inherited a project and I'm not sure what is going on... but 我已经继承了一个项目,但不确定发生了什么...但是

I have the following form items: 我有以下表单项:

<div><label for="email">email:</label><input type="email" id="email" name="email" required ng-model="user.email" ng-minlength=4></div>
<div><label for="userName">Username:</label><input type="text" name="userName" id="userName" ng-model="user.userName" required ng-pattern="/^[A-Za-z0-9 \-_.]*$/" ng-minlength=4></div>
<div><label for="firstName">Vorname:</label><input type="text" name="firstName" id="firstName" ng-model="user.firstName" required  ng-pattern="/^[A-Za-z \-_.]*$/" ng-minlength=3></div>
<div><label for="lastName">Nachname:</label><input type="text" name="lastName" id="lastName" ng-model="user.lastName" required ng-pattern="/^[A-Za-z \-_.]*$/" ng-minlength=4></div>
<div><label for="password1">Passwort:</label><input type="password" name="password1" id="password1" ng-model="user.password1" required ng-minlength=4></div>
<div><label for="password2">Passwort wiederholen:</label><input type="password" name="password2" id="password2" ng-model="user.password2" valid-password2 ng-minlength=4 pw-check="password1"></div>

and in my CSS I have set the following css classes 在我的CSS中,我设置了以下CSS类

/**
*  Form Validation
*/

input.ng-invalid {
  border: 1px solid red;
}
input.ng-valid {
  border: 1px solid green;
}

however when the page loads for the first time the css rules are applied to the empty form items? 但是,当页面首次加载时,css规则将应用于空表单项吗? Thus all the input type="email" or input="text" have a red border applied to them. 因此,所有input type="email"input="text"都应用了红色边框。 The AngularJS version is v1.0.8 AngularJS版本是v1.0.8

Has anyone experienced this before and resolved the issue? 之前有没有人经历过并解决了这个问题?

Use :not pseudo-class, like this: 使用:not伪类,像这样:

input.ng-invalid:not(.ng-pristine) {
  border: 1px solid red;
}

Working example: http://plnkr.co/edit/C38kgE?p=preview 工作示例: http : //plnkr.co/edit/C38kgE?p=preview

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

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