如果填写任何相关的对象字段,如何使ng-required为true?

时间:2016-05-17 09:11:51

标签: javascript angularjs json validation ng-required

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

1 个答案:

答案 0 :(得分: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: ...}个键。

现在,当您拨打此行var numMembers = Object.keys($scope.members).length;时,您会收到numMembers = 2,当您在for周期内评估if条件时,您的第二行{{1}你得到这个:i,但你的成员变量中没有Object.keys($scope.members[1]).length键,因此错误。

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

1

Here是你的掠夺者的工作分叉。