admin管理员组文章数量:1241129
Form fields not being added ng-touched
class, instead remains ng-pristine
This is my form:
<form novalidate class="css-form">
<div class="form-group clearfix">
<label class="col-xs-12 col-sm-6 col-md-4">State Code</label>
<div class="col-xs-12 col-sm-6 col-md-8">
<input type="text" class="form-control" ng-maxlength="2" ng-model="ObjectNew.stateCode" required>
</div>
</div>
<div class="clearfix"></div>
<div class="form-group clearfix">
<label class="col-xs-12 col-sm-6 col-md-4">Zip</label>
<div class="col-xs-12 col-sm-6 col-md-8">
<input type="text" class="form-control" ng-model="ObjectNew.zip" required>
</div>
</div>
<div class="clearfix"></div>
</form>
When I see in inspect element, it does show only ng-pristine
class on each field, even if i went through both fields and leaves null.
It shows ng-invalid
too, which is fine.
Have to add the styles actually:
.css-form input.ng-invalid.ng-touched {
background-color: #FA787E !important;
}
.css-form input.ng-valid.ng-touched {
background-color: #78FA89 !important;
}
Form fields not being added ng-touched
class, instead remains ng-pristine
This is my form:
<form novalidate class="css-form">
<div class="form-group clearfix">
<label class="col-xs-12 col-sm-6 col-md-4">State Code</label>
<div class="col-xs-12 col-sm-6 col-md-8">
<input type="text" class="form-control" ng-maxlength="2" ng-model="ObjectNew.stateCode" required>
</div>
</div>
<div class="clearfix"></div>
<div class="form-group clearfix">
<label class="col-xs-12 col-sm-6 col-md-4">Zip</label>
<div class="col-xs-12 col-sm-6 col-md-8">
<input type="text" class="form-control" ng-model="ObjectNew.zip" required>
</div>
</div>
<div class="clearfix"></div>
</form>
When I see in inspect element, it does show only ng-pristine
class on each field, even if i went through both fields and leaves null.
It shows ng-invalid
too, which is fine.
Have to add the styles actually:
.css-form input.ng-invalid.ng-touched {
background-color: #FA787E !important;
}
.css-form input.ng-valid.ng-touched {
background-color: #78FA89 !important;
}
Share
Improve this question
asked Feb 24, 2015 at 11:08
Fahad KhanFahad Khan
1,6354 gold badges23 silver badges38 bronze badges
4
- 3 Make sure you use Angular 1.3.x version. – dfsq Commented Feb 24, 2015 at 11:15
- Oops! currently using 1.2.26 - what I need to make sure after upgrading to 1.3x ? I mean do I lose something, some support? – Fahad Khan Commented Feb 24, 2015 at 11:18
-
2
Also, remmeber than
ng-touched
class will be added only when the element loses focus, not immediately after it is clicked. – kamituel Commented Feb 24, 2015 at 11:19 - Unlikely. But everything can be. Just upgrage and see if something breaks. – dfsq Commented Feb 24, 2015 at 11:19
1 Answer
Reset to default 13The term pristine
and touched
are not similar. An input will remain pristine
as long as it has not been modified. It is untouched
, as long as it didn't loose focus - Angular doc states that ng-touched
is applied when "the control has been blurred".
So it is possible for an input to be pristine and touched - just click it, and then click somewhere else without typing anything in.
It is also possible for it to be dirty and untouched - click it, type in some text, but do not click anywhere else.
To see that, and see working ng-touched
in action, refer to this jsFiddle.
To make sure it works, use Angular 1.3 as pointed out by @dfsq. Also, use angular-touch
which matches your Angular version. The JSFiddle above works correctly.
本文标签: javascriptngtouched not workingAngularJSStack Overflow
版权声明:本文标题:javascript - ng-touched not working - AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740024632a2220940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论