Validation AngularJS中的验证
我对AngularJS是新手。我正在尝试实现一些验证。作为测试,我正在查看一个基本的登录屏幕。该登录屏幕的代码如下所示:Validation AngularJS中的验证,validation,angularjs,Validation,Angularjs,我对AngularJS是新手。我正在尝试实现一些验证。作为测试,我正在查看一个基本的登录屏幕。该登录屏幕的代码如下所示: <style type="text/css"> input.ng-invalid.ng-dirty { background-color: red; color:white; } </style> <form novalidate name="loginForm" role="form"> <div ng-show="(is
<style type="text/css">
input.ng-invalid.ng-dirty { background-color: red; color:white; }
</style>
<form novalidate name="loginForm" role="form">
<div ng-show="(isDataValid==false)" class="ng-hide">
Please correct the errors on the page.
</div>
<div>
<label for="username">Username</label>
<input type="text" id="username" placeholder="Username" ng-model="username"
required>
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" placeholder="Password" ng-model="password"
required>
</div>
<button type="submit" ng-click="formSubmit(loginForm);">Submit</button>
</form>
<script type="text/javascript">
function loginController($scope) {
$scope.isDataValid = null;
$scope.formSubmit = function(f) {
$scope.isDataValid = f.$valid;
if (f.$invalid) {
return;
}
return false;
};
}
</script>
input.ng-invalid.ng-dirty{背景色:红色;颜色:白色;}
请更正页面上的错误。
用户名
密码
提交
函数loginController($scope){
$scope.isDataValid=null;
$scope.formSubmit=函数(f){
$scope.isDataValid=f.$valid;
如果(f.$无效){
返回;
}
返回false;
};
}
当页面加载时,我希望它处于一个干净的状态(这是有效的)。我希望在用户开始使用某个字段后进行字段级验证(这是有效的)。我希望在用户单击“提交”时进行字段级验证。这部分起作用
我的问题是,如果我在根本不使用字段的情况下按“提交”,则字段上的css类不会得到更新。我相信这是因为田地不是“脏的”。但是,我不知道如何解决这个问题,1)满足我的第一个要求,2)坚持让AngularJS控件远离DOM的想法
有人知道我怎么做吗
非常感谢你 我对你的帖子发表了评论,但我想我应该举个例子
<button type="button" class="btn btn-primary btn-lg" ng-click="submit()" ng-disabled="(createContacts.$dirty && createContacts.$invalid) || createContacts.$pristine"><span class="icon-save"></span> Save Contacts</button>
保存联系人
这是我在名为“createContacts”的表单中使用的“提交”按钮。当表单处于“原始”状态(未输入任何内容)以及表单“脏”和“无效”(已输入信息但表单存在验证错误)时,用户无法使用此按钮
我对你的帖子发表了评论,但我想我应该举个例子
<button type="button" class="btn btn-primary btn-lg" ng-click="submit()" ng-disabled="(createContacts.$dirty && createContacts.$invalid) || createContacts.$pristine"><span class="icon-save"></span> Save Contacts</button>
保存联系人
这是我在名为“createContacts”的表单中使用的“提交”按钮。当表单处于“原始”状态(未输入任何内容)以及表单“脏”和“无效”(已输入信息但表单存在验证错误)时,用户无法使用此按钮
这将帮助您禁用提交按钮,直到表单
$dirty
且字段$valid
这将帮助您禁用提交按钮,直到表单$dirty
且字段$valid