【发布时间】:2012-08-28 09:02:37
【问题描述】:
我所拥有的表单的默认验证按预期工作。但是,当用户输入有效的电子邮件地址和最少三个字符的密码时,这并不意味着登录凭据有效。
所以我的问题是:
我怎样才能将模型 email 和 password 设置为无效后 服务器端验证,因此输入字段获得类 ng-invalid 而不是 ng-valid。
我当前的代码
function IndexCtrl( $scope, $http )
{
$scope.form = {};
$scope.submitLogin = function ()
{
$http.post( '/api/auth/login', $scope.form ).success( function( data )
{
if ( !data.success )
{
$scope.form.errors = [ data ];
// here I also want to mark the models 'email' and 'password' as invalid, so they both get the class 'ng-invalid'
}
else
{
$location.path( '/' );
}
});
};
}
【问题讨论】:
-
你可以简单地使用 $setValidity 方法吗?
-
完全不相关,但是当我的 AJAX 调用返回错误时,我将状态码设置为 400。这样您就不需要检查或设置
data.success,而只需在错误回调中处理错误。
标签: javascript angularjs