4

默认情况下,第一个人的详细信息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>

4

1 回答 1

1

你几乎是对的——你的代码有一个小问题。

var numMembers = Object.keys($scope.members).length; // <-- length of keys
  for (i = 0; i < numMembers; i++) {  // <-- iterate over length, not actual indexes
    if (Object.keys($scope.members[i]).length <= 2) {
      $scope.person[i].isRequired = true;
    }
  }

在您的代码中,您计算​​为由您i的键创建的数组的索引$scope.members。想象一下,您填写FirstLast表格,获取$scope.members = {0: ..., 2: ...}密钥。

现在,当您调用此 line 时var numMembers = Object.keys($scope.members).length;,您会得到numMembers = 2,并且在您的for循环中评估您的if条件时,对于您的第二个i,您会得到这个: ,但是您的 members 变量Object.keys($scope.members[1]).length中没有键,因此出现错误。1

要解决此问题,请将 for 循环基于现有键数组本身:

var members = Object.keys($scope.members);
  for (i = 0; i < members.length; i++) {
    if (Object.keys($scope.members[members[i]]).length <= 2) { // Check key length of the existing keys only.
      $scope.person[members[i]].isRequired = true;
    }
  }

是您的 plunker 的工作分叉。

于 2016-05-17T09:51:06.840 回答