javascript – 如果填写了任何相关的对象字段,如何使ng-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>

最佳答案 你得到的几乎是正确的 – 你的代码存在一个小问题.

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的键创建的数组的索引.想象一下,你填写了First和Last表格,获得了$scope.members = {0:…,2:…}键.

现在,当你调用这行var numMembers = Object.keys($scope.members).length;时,你得到numMembers = 2,当你在for循环中你评估你的if条件,对于你的第二个i,你得到这个: 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;
    }
  }

Here是你的plunker的工作叉.

点赞