Reputation: 2166
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);
};
}]);
Upvotes: 0
Views: 122
Reputation: 136124
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
Upvotes: 1