简体   繁体   中英

accessing ng-form elements/values with ng-repeat

I'm looking for the best way to pass non-binding (correct term?) form data to a controller method. How would I go about accessing the object of the form and it's values?

html

<html ng-app="app">
<body ng-controller="MainController as main">
<ng-form name="add_lead_form">
    <div ng-repeat="label in form.lead_labels track by $index" ng-show="label.visible">
        <div class="input-group lead-data">
            <span class="input-group-addon">{{getLabel(label)}}</span>
             <input ng-model="add_lead_form.addleadValue[$index]" name="addleadValue" type="text" class="form-control" aria-describedby="basic-addon1">
             <input type="hidden" ng-model="add_lead_form.addleadLabelId[$index]" name="add_lead_form.addleadLabelId" ng-value="label.id" />
        </div>
    </div>


    <div class="pull-right"><button ng-click="addLead()" class="btn btn-primary btn-lg">Save Lead</button></div>
</ng-form>
</body>
</html>

app.js

var app = angular.module('app', []);

app.controller('MainController', ['$scope', function($scope) {
    $scope.addLead = function() {
        console.log($scope.add_lead_form);
    };

}]);

You could not access form inside ng-repeat which has been declared in parent div, For creating proper form you could add new innerForm inside your ng-repeat , You must need to change your html structure by adding new innerForm

<div ng-repeat="label in form.lead_labels track by $index" ng-show="label.visible">
    <ng-form name="innerForm">
        <div class="input-group lead-data">
            <span class="input-group-addon">{{getLabel(label)}}</span>
            <input ng-model="add_lead_form.addleadValue[$index]" name="innerForm.addleadValue" type="text" class="form-control" aria-describedby="basic-addon1">
            <input type="hidden" ng-model="add_lead_form.addleadLabelId[$index]" name="innerForm.addleadLabelId" ng-value="label.id" />
        </div>
    </ng-form>
</div>

Give a try by using above code, Thanks

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