gpt4 book ai didi

angularjs - ng-repeat 中的 Angular 验证不起作用

转载 作者:行者123 更新时间:2023-12-02 11:27:19 24 4
gpt4 key购买 nike

我很难使用 ng-repeat 语句进行验证。我有以下代码。我只是希望当名称字段为空时将 has-error 添加到 div.form-group 中。我似乎无法让它发挥作用。有什么想法吗?

My Attempt in fiddler

<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 指令的元素:

<tr ng-repeat="order in orders" ng-form="innerForm">

演示: http://jsfiddle.net/YTR95/

关于angularjs - ng-repeat 中的 Angular 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22642536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com