Javascript 如何使用<;提交和验证表格;a>; 用户名和密码是必需的 var app=angular.module(“loginApp”,“ngMessages”); 应用控制器(“loginCtrl”,功能($scope){ $scope.username=“”; $scope.password=“”; $scope.submitForm=function(){ }; });
现在我有了一个上面显示的登录页面,我正在尝试使用ngMessages进行验证Javascript 如何使用<;提交和验证表格;a>; 用户名和密码是必需的 var app=angular.module(“loginApp”,“ngMessages”); 应用控制器(“loginCtrl”,功能($scope){ $scope.username=“”; $scope.password=“”; $scope.submitForm=function(){ }; });,javascript,angularjs,Javascript,Angularjs,现在我有了一个上面显示的登录页面,我正在尝试使用ngMessages进行验证 如果我必须使用表单之外的而不是按钮来提交它,我应该怎么做 如何确保用户名或密码为空时显示错误消息,并且仅在用户提交表单后显示 如何防止用户使用重新提交表单 您可以通过以下方式检查控制器功能中的验证结果: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial
而不是按钮来提交它,我应该怎么做
重新提交表单您可以通过以下方式检查控制器功能中的验证结果:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" type="text/css" href="login.css"> -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-messages.min.js"></script>
</head>
<body ng-app="loginApp">
<div class="container">
<div class="login_logo">
</div>
<div class="form_container" ng-controller="loginCtrl" >
<div class="error_msg" ng-if="form_login.username.$dirty" ng-messages="form_login.username.$error">
<div class="alertmsg" ng-message="required">Username and password are required</div>
</div>
<div class="form_left">
<form class="form_login" name="form_login" ng-submit="submitForm()" novalidate>
<div class="usr"><input id="username" name="username" ng-model="username" type="text" autofocus="autofocus" required /></div>
<div class="psw"><input id="password" name="password" ng-model="password" type="password" required /></div>
</form>
</div>
<div class="form_right">
<a class="submit" href="" ng-click="submitForm()">submit</a>
</div>
<div class="clear"></div>
</div>
</div>
<script type="text/javascript">
var app=angular.module("loginApp",["ngMessages"]);
app.controller("loginCtrl", function($scope){
$scope.username = "";
$scope.password = "";
$scope.submitForm=function(){
};
});
</script>
</body>
</html>
angularjs中的表单将自动验证。对于所有表单成员,您可以检查以仅在表单提交后显示错误消息,请添加变量“$scope.submitted=true;”。 为了防止重新提交,可以禁用提交按钮。 有关详细说明,请参阅以下链接。
希望有帮助。没有意义。解释为什么不使用表单按钮而不是
。将表单标记移动到左包装和右包装是一个很小的问题。
if ($scope.form_login.$valid) {
...
}