admin管理员组文章数量:1399901
I tried to put green background if it is edited and valid.
But, even for invalid also it is showing green background. On invalid it should show the yellow background with red text.
Thanks in advance for giving me the solution.
html
<div ng-app="myApp" ng-controller="formCtrl">
<form name="inputform">
<div ng-class="{'has-success': inputform.email.$dirty && inputform.email.$valid, 'has-error': inputform.email.$dirty && inputform.email.$invalid}">
<label for="exampleInputEmail1">Email</label>
<input type="text" name="email" ng-model="data.email" id="exampleInputEmail1" />
</div>
</form>
{{data}}
</div>
javascript
var module = angular.module("myApp", []);
module.controller("formCtrl", ['$scope', function($scope){
$scope.data = {};
}]);
css
.has-error {
color: red;
background-color: red;
}
.has-success {
color: black;
background-color: green;
}
I tried to put green background if it is edited and valid.
But, even for invalid also it is showing green background. On invalid it should show the yellow background with red text.
Thanks in advance for giving me the solution.
html
<div ng-app="myApp" ng-controller="formCtrl">
<form name="inputform">
<div ng-class="{'has-success': inputform.email.$dirty && inputform.email.$valid, 'has-error': inputform.email.$dirty && inputform.email.$invalid}">
<label for="exampleInputEmail1">Email</label>
<input type="text" name="email" ng-model="data.email" id="exampleInputEmail1" />
</div>
</form>
{{data}}
</div>
javascript
var module = angular.module("myApp", []);
module.controller("formCtrl", ['$scope', function($scope){
$scope.data = {};
}]);
css
.has-error {
color: red;
background-color: red;
}
.has-success {
color: black;
background-color: green;
}
Share
Improve this question
edited Jul 21, 2016 at 8:37
Shashank Agrawal
25.8k11 gold badges96 silver badges125 bronze badges
asked Jul 21, 2016 at 8:18
ManojManoj
971 gold badge4 silver badges11 bronze badges
1
- 1 You didn't add any validation constraints, so what is invalid you expect then? Of course, it's always valid. – dfsq Commented Jul 21, 2016 at 8:27
1 Answer
Reset to default 4There is no validation applied to that input element so it is valid all the time. Try the below form, I've applied the required
and email
validation:
(It will only be green if a proper email address in entered)
var module = angular.module("myApp", []);
module.controller("formCtrl", ['$scope', function($scope){
$scope.data = {};
}]);
.has-error {
color: red;
background-color: red;
}
.has-success {
color: black;
background-color: green;
}
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="formCtrl">
<form name="inputform">
<div ng-class="{'has-success': inputform.email.$valid, 'has-error': inputform.email.$invalid}">
<label for="exampleInputEmail1">Email</label>
<input type="email" name="email" required ng-model="data.email" id="exampleInputEmail1" />
</div>
</form>
{{data}}
</div>
Alternatively, you can use this library to provide Bootstrap form validation in simple way.
本文标签: javascriptngclass validation is not working in AngularJSStack Overflow
版权声明:本文标题:javascript - ng-class validation is not working in AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744187981a2594383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论