我想创建函数(在angularjs控制器中),在单击按钮时将特定类添加到空输入的父div。当我在输入中输入内容时删除该类。
我在这里有一个有效的例子:http://jsfiddle.net/fUdLv/
HTML:
<div ng-app="authorization">
<div ng-controller="authCtrl as auth">
<div ng-class="{'error':auth.needEmail}">
<input type="text" ng-model="auth.email" ng-keypress="auth.clearEmail()">
</div>
<div ng-class="{'error':auth.needPassword}">
<input type="password" ng-model="auth.password" ng-keypress="auth.clearPassword()">
</div>
<div>
<button ng-click="auth.signin()">Sign in</button>
</div>
</div>
</div>
使用Javascript:
angular.module('authorization', [])
.controller('authCtrl', function() {
this.needEmail = false;
this.needPassword = false;
this.signin = function pay() {
if (!this.email){
this.needEmail = true;
}
if (!this.password){
this.needPassword = true;
}
};
this.clearEmail = function(){
this.needEmail = false;
}
this.clearPassword = function(){
this.needPassword = false;
}
});
但我确定这是非常糟糕的代码,因为我有一个特殊的功能来处理特定的输入。我如何推广这个函数来处理每个输入(例如,如果我有三个或四个输入,那么为每个输入创建函数不是非常聪明的解决方案)