总体而言,AngularJS noob总共在这里寻找答案。
我的页面设置如下:
<form name="mainForm" class="content" ng-app="pathForm" ng-controller="FormControl" novalidate>
<div class="full-page open" id="form1">
<!-- Form 1 content here -->
</div>
<div class="full-page" id="form2">
<!-- Form 2 content here -->
</div>
<div class="full-page" id="form3">
<!-- Form 3 content here -->
</div>
<div class="full-page" id="form4">
<!-- Form 4 content here -->
</div>
</form>
这些表单中的每一个都有自己的一组需要验证的输入。我已经能够在所有四种表单中进行验证,因为我设置了一个涵盖所有4种表单的包含形式的ng-app。在提交时,脚本会删除“打开”状态。从开放表单开始,循环到enxt表单并在该表单上添加开放类。
如何设置它以便可以单独验证这些表单中的每一个?
提前致谢。
答案 0 :(得分:10)
您需要ngForm directive和FormController。
此指令中每个带有名称的ng-form指令都会向$scope
添加属性。您可以在提交按钮单击调用的函数中访问此属性(它是对象,FormController的实例)。
此对象中有一些属性和方法。您需要使用属性$valid
(或$invalid
)来获取表单的验证状态。
HTML:
<ng-form name="form1"></ng-form>
<ng-form name="form2"></ng-form>
<button ng-click="submit();">Submit</button>
JS:
$scope.submit = function () {
if ($scope.form1.$valid && $scope.form2.$valid) {}
}
自定义验证器 如果没有内置验证器,您可以添加自己的自定义验证器。