0
我想将此类视图中的值从此视图传递给此窗体的角度js中的控制器。我不希望以这种方式对其进行硬编码。如何以适当的方式完成?将NG模型从视图传递到控制器角度js
angular.module('user').controller('UsersController', ['$scope', '$stateParams', 'Users',
\t function($scope, $stateParams, Orders) {
\t \t $scope.create = function() {
\t \t \t var user = new Users({
\t \t \t \t child: [
\t \t \t \t \t { columnA: child[0].columnA, columnB: child[0].columnB, columnC: child[0].columnC },
\t \t \t \t \t { columnB: child[1].columnA, columnB: child[1].columnB, columnC: child[1].columnC },
\t \t \t \t \t ...
\t \t \t \t \t { columnC: child[10].columnA, columnB: child[10].columnB, columnC: child[10].columnC }
\t \t \t \t ]
\t \t \t });
\t \t }
\t }
});
<form data-ng-submit="create()">
<input type="text" data-ng-model="child[0].columnA">
<input type="text" data-ng-model="child[0].columnB">
<input type="text" data-ng-model="child[0].columnC">
<input type="text" data-ng-model="child[1].columnA">
<input type="text" data-ng-model="child[1].columnB">
<input type="text" data-ng-model="child[1].columnC">
......
<input type="text" data-ng-model="child[10].columnA">
<input type="text" data-ng-model="child[10].columnB">
<input type="text" data-ng-model="child[10].columnC">
</form>
这将是,如果可能上面的函数执行的可重复使用的指令更好。
$scope.create = function() {
child: toJSON(child);
}
function toJSON(var a) {
//automatically search through the view for ng-model with child[index].columnName and change to the form above.
}
不清楚你在问什么。你试过用'ng-repeat'吗? – charlietfl
@charlietfl您好,我目前没有问题。这是从视图到控制器的用户提交表单的后表单。在这种情况下,如何简化控制器中的分配数据ng模型进程? – stackdisplay
仍然不理解问题。你意识到'ng-model'会自动创建你已经存在的所有对象,如果它们不存在的话? – charlietfl