Angular check in ng-repeat does not work

It's hard for me to get confirmation to work with the 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 can't seem to work. Any thoughts?

My attempt at a violinist

<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'
    }];

});
+4
source share
1 answer

Move the directive ngFormto the element containing the directive ngRepeat:

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

Demo: http://jsfiddle.net/YTR95/

+8
source

Source: https://habr.com/ru/post/1533384/


All Articles