在AngularJS中使用ng类添加条件CSS类
我有一个简单的问题。我想在表单提交的输入中更新我的类在AngularJS中使用ng类添加条件CSS类,css,angularjs,forms,Css,Angularjs,Forms,我有一个简单的问题。我想在表单提交的输入中更新我的类 <div class="form-group"> <input class="form-control" ng-class="{'has-error': signup.$submitted && signup.name.$invalid}" type="text" name="name" ng-model="formData.name" required minlength="2" placeholder
<div class="form-group">
<input class="form-control" ng-class="{'has-error': signup.$submitted && signup.name.$invalid}" type="text" name="name" ng-model="formData.name" required minlength="2" placeholder="Full Name">
</div>
当我通过ng单击
调用此函数执行提交时,我的注册。$submitted
仍保持为false
。我还从这个Plunker那里得到了一个参考:
那个Plunkr更新了它的类。为什么我的不也这么做?我做错什么了吗?您正在设置作用域。$已在控制器中提交,但ng类正在使用注册。$已提交。我的
表单
的名称是注册
。使用formName.$control.
的格式不是很正确吗?就像我以前验证输入的方式一样,例如signup.name.$error.required
?另外,使用scope.$submitted
不起作用。在视图中使用$submitted代替scope.$submitted谢谢!我已经做了。:)
spinnrApp.controller('FormController', ['$scope', '$http', '$state', function (scope, http, state){
// get list of cities and store it to select
http.get('cities.json').success(function(data){
scope.cities = data;
})
// we will store all our form data in this object
scope.formData = {};
// function to process the form
scope.processForm = function(isValid) {
scope.$submitted = true;
if(isValid && state.$current=='registration.spinnrapp') {
state.go('registration.artist');
} else if(isValid && state.$current=='registration.artist') {
state.go('registration.share');
} else if(!isValid && state.$current=='registration.artist') {
alert('Please make sure you have selected an artist.');
} else if(!isValid && state.$current=='registration.spinnrapp') {
return;
}
};
}]);