默认情况下,第一个人的详细信息required="true"
,但其他人的表格required="true"
基于其填写或未填写的任何文件。
该片段的当前问题是,当最后一个人的任何一个字段被填充时,它不会被验证。
演示:http ://plnkr.co/edit/WUpybR1tNq5QFjlTB2E7?p=preview
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.person = [{isRequired: true}, {isRequired: false}, {isRequired: false}, {isRequired: false}];
$scope.members = {};
$scope.submitForm = function() {
console.clear();
if (typeof $scope.members != 'undefined' && Object.keys($scope.members).length) {
var numMembers = Object.keys($scope.members).length;
for (i = 0; i < numMembers; i++) {
if (Object.keys($scope.members[i]).length <= 2) {
$scope.person[i].isRequired = true;
}
}
} else {
$scope.person = [{isRequired: true}, {isRequired: false}, {isRequired: false}, {isRequired: false}];
}
}
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.9"></script>
<div ng-app="plunker" ng-controller="MainCtrl" class="container">
<form name="myForm" ng-submit="submitForm(myForm.$valid)" novalidate>
<h4>First person</h4>
<div class="form-group" ng-class="{ 'has-error' : (myForm.$submitted && myForm.firstnameOne.$invalid) || (!myForm.firstnameOne.$pristine && myForm.firstnameOne.$invalid)}">
<label>First Name</label>
<input type="text" class="form-control" name="firstnameOne" placeholder="First Name" ng-model="members[0].fname" ng-required="person[0].isRequired">
</div>
<div class="form-group" ng-class="{ 'has-error' : (myForm.$submitted && myForm.lastnameOne.$invalid) || (!myForm.lastnameOne.$pristine && myForm.lastnameOne.$invalid)}">
<label>Last Name</label>
<input type="text" class="form-control" name="lastnameOne" placeholder="Last Name" ng-model="members[0].lname" ng-required="person[0].isRequired">
</div>
<h4>Second person</h4>
<div class="form-group" ng-class="{ 'has-error' : (myForm.$submitted && myForm.firstnameSecond.$invalid) || (!myForm.firstnameSecond.$pristine && myForm.firstnameSecond.$invalid)}">
<label>First Name</label>
<input type="text" class="form-control" name="firstnameSecond" placeholder="First Name" ng-model="members[1].fname" ng-required="person[1].isRequired">
</div>
<div class="form-group" ng-class="{ 'has-error' : (myForm.$submitted && myForm.lastnameSecond.$invalid) || (!myForm.lastnameSecond.$pristine && myForm.lastnameSecond.$invalid)}">
<label>Last Name</label>
<input type="text" class="form-control" name="lastnameSecond" placeholder="Last Name" ng-model="members[1].lname" ng-required="person[1].isRequired">
</div>
<h4>Last person</h4>
<div class="form-group" ng-class="{ 'has-error' : (myForm.$submitted && myForm.firstnameThird.$invalid) || (!myForm.firstnameThird.$pristine && myForm.firstnameThird.$invalid)}">
<label>First Name</label>
<input type="text" class="form-control" name="firstnameThird" placeholder="First Name" ng-model="members[2].fname" ng-required="person[2].isRequired">
</div>
<div class="form-group" ng-class="{ 'has-error' : (myForm.$submitted && myForm.lastnameThird.$invalid) || (!myForm.lastnameThird.$pristine && myForm.lastnameThird.$invalid)}">
<label>Last Name</label>
<input type="text" class="form-control" name="lastnameThird" placeholder="Last Name" ng-model="members[2].lname" ng-required="person[2].isRequired">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<br><hr>
<h4>Data</h4>
<pre>{{members | json}}</pre>
<h4>Validation</h4>
<pre>{{person | json}}</pre>
</div>