如果填充了任何相关对象字段,如何使 ng-required 为真?
how to make ng-required true if any of the related object field filled?
默认情况下,第一个人的详细信息 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>
你几乎答对了 - 你的代码有一个小问题。
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 的工作叉。
默认情况下,第一个人的详细信息 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>
你几乎答对了 - 你的代码有一个小问题。
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 的工作叉。