我正在寻找将非绑定(bind)(正确的术语?)表单数据传递给 Controller 方法的最佳方法。我将如何访问表单的对象及其值?
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
已在父 div 中声明,为了创建正确的表单,您可以在 ng-repeat
中添加新的 innerForm
,您必须通过添加新的 来更改 html 结构>内部表单
<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>
使用上面的代码尝试一下,谢谢
关于javascript - 使用 ng-repeat 访问 ng-form 元素/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29356148/