【发布时间】:2017-05-30 22:06:55
【问题描述】:
我正在做 angular Project。我有两个 div。在一个 div 中有通过 ng-repeat 生成的动态输入。最初,两个输入将可用,但如果用户单击添加多于动态,则将通过 ng-repeat 生成两个输入并且在另一个 div 中已经存在两个输入类型文本(不是通过 ng-repeat 生成)。
我面临的问题是如何在提交时获取两个 div 输入的值。请帮助我解决如何在提交时一次获取所有数据。我的代码:
var addnew = angular.module('dump',[]);
addnew.controller('dumpcontroller', function($scope)
{
$scope.choices = [{id: 'choice1'}];
$scope.addNewChoice = function() {
var newItemNo = $scope.choices.length+1;
$scope.choices.push({'id':'choice'+newItemNo});
};
$scope.removeChoice = function() {
var lastItem = $scope.choices.length-1;
$scope.choices.splice(lastItem);
};
});
<html>
<head>
<script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
</head>
<body ng-app="dump" ng-controller="dumpcontroller">
<div class="divone" data-ng-repeat="choice in choices">
<input type="text" placeholder="Name" ng-model="choice.childname">
<input type="text" placeholder="Name" ng-model="choice.childbirth">
<button ng-click="removeChoice()">-</button> //To remove the field dynamically.
</div>
<button class="remove" ng-click="addNewChoice()">Add more</button> //To add the more childname and childbirth field.
<div class="divtwo">
<input type="text" placeholder="Name" ng-model="choice.firstname">
<input type="text" placeholder="Name" ng-model="choice.lastname">
</div>
<button type="button" id="submit" name="submit" ng-click="famupdate(choices)">Submit</button>
</body>
</html>
现在我想在控制器中单击时获取两个 div 的所有详细信息。我该如何实现这一点。帮帮我。
谢谢你的期待。
【问题讨论】:
-
在 plnkr 中分享您的代码。
-
我添加了sn-p
-
你想要两个在同一个数组中?
-
使用带偏移量的 ng-repeat,从
$index= 1 开始 ng-repeat,并在 div 2 中将该数组的第一个对象显示为静态 -
你能用一个例子解释一下或者修改我的sn-ps。感谢你在期待。 @EbinManuval
标签: angularjs angularjs-scope angularjs-ng-repeat ng-repeat