简体   繁体   中英

Angularjs ng-submit not working inside ng-repeat form

I have this form

<tr ng-repeat="quote in quotes">
    <form ng-submit="submit()" name="qut">
        <td class="text-left">
            {[{ quote.business_name }]}
        </td>
        <td class="text-left">
            <span ng-if="quote.quote">
                {[{ quote.quote }]}
            </span>
            <span ng-if="!quote.quote">
                <input ng-model="qt" class="form-control" type="text" name="quote" />
            </span>
        </td>
        <td class="text-left">
            <span ng-if="quote.status==1">
                <input type="submit" class="btn btn-out" value="Quote" />
            </span>
        </td>
    </form>
</tr>

In my controller I have

$scope.submit = function() {
        console.log('form');
    };

If I change ng-submit="submit()" to ng-click="submit()" in button it works, not sure why I am unable to submit the form

Because multiple same form names are being created. What you should do is you can create dynamic form names inside ng-repeat.

<tr ng-repeat="quote in quotes">
    <form ng-submit="submit(qut{{$index}}.$valid)" name="qut{{$index}}">
        <td class="text-left">
            {[{ quote.business_name }]}
        </td>
        <td class="text-left">
            <span ng-if="quote.quote">
                {[{ quote.quote }]}
            </span>
            <span ng-if="!quote.quote">
                <input ng-model="quote.quote" class="form-control" type="text" name="quote{{$index}}" />
            </span>
        </td>
        <td class="text-left">
            <span ng-if="quote.status==1">
                <input type="submit" class="btn btn-out" value="Quote" />
            </span>
        </td>
    </form>
</tr>



$scope.submit = function(value) {
    console.log('form',value);
};

The problem is that you have an illegal html structure by nesting a table > tr element with a form . That causes the inner input[type=submit] not to identify his parent form and trigger the submit.

I could get your example working by replacing tables and tr with div and td with spans .

 angular.module('myApp', []) .controller('myController', function($scope) { $scope.quotes = [{ business_name: "business_name 1", quote: "quote1", status: 1 }, { business_name: "business_name 2", quote: "quote2", status: 1 }] $scope.submit = function() { console.log('form'); }; }); angular.element(document).ready(function() { angular.bootstrap(document, ['myApp']); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> <div ng-controller="myController"> <div ng-repeat="quote in quotes"> <form ng-submit="submit()" name="qut{{$index}}"> <span class="text-left"> {{ quote.business_name }} </span> <span class="text-left"> <span ng-if="quote.quote"> {{ quote.quote }} </span> <span ng-if="!quote.quote"> <input ng-model="qt" class="form-control" type="text" name="quote" /> </span> </span> <span class="text-left"> <span ng-if="quote.status==1"> <input type="submit" class="btn btn-out" value="Quote" /> </span> </span> </form> </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM