如果填写表单时电子邮件为空,我想防止 http-post 的默认操作为“/sign Up”。
控制器代码:-
$scope.signUp = function() {
if($scope.email = null);
preventdefault;
}
html(玉):-
form(name="input", action="/signUp", method="post")
input(type="submit", value="submit")
当你为表单指定了action属性时,AngularJS将不会执行preventDefault。
如果删除它并添加 ng-submit 代替:
<form name="myForm" method="post" ng-submit="signUp(myForm)" novalidate>
<input type="email" name="email" ng-model="newSignup.email" required>
<button type="submit">sign up</button>
</form>
在这种情况下,表单将始终具有 PreventDefault ,并且在提交时将调用 $scope.signUp() 函数,您可以在其中继续向后端 /signup 发送 ajax post 或进一步验证。
请注意,通过在输入上使用正确的验证属性(例如 type="email" 和 required),Angularjs 将为您执行一些基本验证。
你可以有一个额外的ng-disabled="!myForm.$valid"
在提交按钮上,以在电子邮件未正确输入时保持按钮禁用状态。
通过在输入上使用 ng-model (如我的示例中所示),您的范围将得到$scope.newSignup
您可以在 signUp() 函数中检查该对象以进行进一步验证:
$scope.signUp = function(htmlForm) {
if ($scope.newSignup.email !== '[email protected]') {
return false; // you should really show some info to the user
}
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)