ng-repeat中的角度验证不起作用 [英] Angular validation in an ng-repeat not working

查看:95
本文介绍了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中的角度验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆