本文介绍了ng-repeat中的角度验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难使用ng-repeat语句使验证生效.我有以下代码.我只想在名称字段为空时将has-error添加到div.form-group中.我似乎无法使它工作.有什么想法吗?
I'm having difficulty getting the validation to work using an ng-repeat statement. I have the following code. I just want the has-error to be added to the div.form-group when the name field is empty. I cant seem to get it work. Any thoughts?
<div ng-app="app" ng-controller="bookController">
<form name="myForm" ng-submit="submitMe(this)" novalidate>
<table class="table">
<tr ng-repeat="order in orders">
<ng-form name="innerForm">
<td>{{ order.id }}</td>
<td>
<div class="form-group" ng-class="{ 'has-error' : innerForm.name.$invalid && submitted }">
<input type="input" ng-model="order.name" name="name" required />
</div>
</td>
</ng-form>
</tr>
</table>
<input type="submit" value="submit" ng-click="submitted = true" />
</form>
<pre>
{{ orders | json }}
</pre>
</div>
var app = angular.module("app", []);
app.controller("bookController", function ($scope) {
$scope.submitMe = function(form) {
console.log(form);
};
$scope.orders = [{
id: 1,
name: ''
}, {
id: 2,
name: 'hat'
}];
});
推荐答案
将ngForm
指令移至包含ngRepeat
指令的元素:
Move the ngForm
directive to the element that contains the ngRepeat
directive:
<tr ng-repeat="order in orders" ng-form="innerForm">
演示: http://jsfiddle.net/YTR95/
这篇关于ng-repeat中的角度验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!