动态添加按钮,这些按钮也会动态添加文本框 AngularJS
Add buttons dynamically which also add textboxes dynamically AngularJS
我有这个JSFiddle代码:
http://jsfiddle.net/rnnb32rm/285/
<div ng-app="angularjs-starter" ng-controller="MainCtrl">
<fieldset data-ng-repeat="choice in choicesA">
<input type="text" ng-model="choice.name" name="" placeholder="Enter name">
<button class="addfields" ng-click="addNewChoice()">Add fields</button>
<button class="remove" ng-click="removeChoice()">-</button>
</fieldset>
<div id="choicesDisplay">
{{ choicesA }} <br/>
{{ choicesB }}
</div>
</div>
.JS:
var app = angular.module('angularjs-starter', []);
app.controller('MainCtrl', function($scope) {
$scope.choicesA = [{id: 'choice1'}, {id: 'choice2'}];
$scope.choicesB = [];
$scope.addNewChoice = function() {
var newItemNo = $scope.choicesA.length+1;
$scope.choicesA.push({'id':'choice'+newItemNo});
};
$scope.removeChoice = function() {
var lastItem = $scope.choicesA.length-1;
$scope.choicesA.splice(lastItem);
};
});
如您所见,我有一个函数addNewChoice()
将对象添加到数组choicesA
,然后根据choicesA
数组上的对象编号添加文本框。
只有当我单击第一个fieldset
上的Add fields
按钮时,我才需要将文本框添加到第一个fieldset
,并且我在那些生成的文本框上写入的数据被绑定并添加到choicesB
数组中的单独对象。 所有其他Add fields
按钮也是如此(因此每个Add field
按钮只能将文本框添加到自己的fieldset
标签中), 它也根据choicesA
数组中的对象数量生成。
我什么都试过了,只是想不通。如果有点不清楚,我可以解释更多。提前谢谢你。
编辑:谢谢大家的大力帮助,让我解释更多:
我有一个Spring REST API和两个名为Resource&Action的Java
对象(JPA实体),对象Resource包含一个操作列表,Action包含对资源的引用。
当我加载一个页面时,我通过一个名为 choicesA
的 $http.get() 方法从数据库中返回了一个已经保存的Resource
对象的数组,数组的结构是这样的:
[
{"idResource":1, "nameResource": "resName1"},
{"idResource":2, "nameResource": "resName2"}
......etc depends oh how much rows I got from the DB
]
我有另一个方法$http.post(),它发布一个名为choicesB
单独的非嵌套数组的Action
对象数组。数组结构是这样的:
[
{"idAction":1, "nameAction":"nameAction1", "resource":
{"idResource":1, "nameResource": "resName1"},
{"idAction":2, "nameAction":"nameAction2", "resource":
{"idResource":2, "nameResource": "resName2"},
..
}
{...},
{...}
...
]
所以 choicesA
数组包含我用 $http.get() 得到的Resource
对象,然后我想填充choicesB
数组中的Action
对象,然后使用 $http.post() 保存数组,每个Action
都应该包含一个Resource
对象。例如,如果我单击以在第一个fieldset
标签中添加更多操作,则意味着我想填充数组中的第一个Action
对象choicesB
并为其分配位于choicesA
数组等中的第一个Resource
对象。
我希望能够决定操作的数量并填写它们,然后将它们保存到choicesB
数组中。 但是每个动作都与我描述的特定Resource
对象相关。
我希望现在清楚了,对不起,再次感谢你。
也许我误解了你的问题。也许这将有助于解决您的问题。
jsfiddle上的活生生的例子。
var app = angular.module('angularjs-starter', []);
app.controller('MainCtrl', function($scope) {
$scope.choicesA = [{
id: 'choice1',
choicesB:[]
}, {
id: 'choice2',
choicesB:[]
}];
$scope.addNewChoice = function() {
var newItemNo = $scope.choicesA.length + 1;
$scope.choicesA.push({
'id': 'choice' + newItemNo,
choicesB:[]
});
};
$scope.removeChoice = function(ind) {
$scope.choicesA.splice(ind,1);
};
$scope.addNewChoiceB = function(choice) {
var newItemNo = choice.choicesB.length + 1;
choice.choicesB.push({
'id': 'choice' + newItemNo
});
};
$scope.removeChoiceB = function(choice,ind) {
choice.choicesB.splice(ind,1);
};
});
fieldset {
background: #FCFCFC;
padding: 16px;
border: 1px solid #D5D5D5;
}
.addfields {
margin: 10px 0;
}
#choicesDisplay {
padding: 10px;
background: rgb(227, 250, 227);
border: 1px solid rgb(171, 239, 171);
color: rgb(9, 56, 9);
}
.remove {
background: #C76868;
color: #FFF;
font-weight: bold;
font-size: 21px;
border: 0;
cursor: pointer;
display: inline-block;
padding: 4px 9px;
vertical-align: top;
line-height: 100%;
}
input[type="text"],
select {
padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="angularjs-starter" ng-controller="MainCtrl">
<button class="addfields" ng-click="addNewChoice()">Add choice</button>
<fieldset data-ng-repeat="choice in choicesA">
<input type="text" ng-model="choice.name" name="" placeholder="Enter name">
<button class="remove" ng-click="removeChoice($index)">-</button>
<button class="addfields" ng-click="addNewChoiceB(choice)">Add fields</button>
<div data-ng-repeat="choiceb in choice.choicesB">
<input type="text" ng-model="choiceb.name" name="" placeholder="Enter field">
<button class="remove" ng-click="removeChoiceB(choice,$index)">-</button>
</div>
</fieldset>
<div id="choicesDisplay">
<pre>choicesA = {{ choicesA }}</pre>
<pre data-ng-repeat="choiceb in choicesA">choicesB = {{ choiceb.choicesB }}</pre>
</div>
</div>
更新
jsfiddle上的活生生的例子。
您要做的是拥有 2 个嵌套数组。
然后你会嵌套ng-repeat
.您可以通过将该数组作为函数的参数传递来跟踪哪个数组
视图
<fieldset data-ng-repeat="group in choices">
<div ng-repeat="choice in group">
<input type="text" ng-model="choice.name" name="" placeholder="Enter name">
<button class="addfields" ng-click="addNewChoice(group)">Add fields</button>
<button class="remove" ng-click="removeChoice(group)">-</button>
</div>
</fieldset>
.JS
$scope.choices = [
// first group
[{id: 'choice1'}, { id: 'choice2'}],
//second group
[{}]
];
$scope.addNewChoice = function(group) {
var newItemNo = group.length + 1;
group.push({
'id': 'choice' + newItemNo
});
};
$scope.removeChoice = function(group) {
group.pop();
};
请注意,您需要稍微修改一下ID
系统。通常无论如何这都会来自服务器
演示
<div ng-app="angularjs-starter" ng-controller="MainCtrl">
<fieldset>
<input data-ng-repeat="choice in choicesA" type="text" ng-model="choice.name" name="" placeholder="Enter name">
<button class="addfields" ng-click="addNewChoice()">Add fields</button>
<button class="remove" ng-click="removeChoice()">-</button>
</fieldset>
您的要求的第一部分通过以下方式解决,文本框已添加到特定的字段集中,而我不清楚第二部分要求将您的 htmlcode 替换为此
- 将按钮添加到新元素中
- 使用加号按钮添加多个文本框,并通过PHP提交
- 复选框/单选按钮-添加所选项目的总价
- 将自定义按钮添加到现有网站
- 单击按钮添加HTML表单
- 将sharethis按钮添加到chrome扩展popup.html
- 从自定义DocuSign按钮添加信封自定义字段
- 我尝试将引导下拉按钮添加到 但下拉菜单不起作用.我该怎么做
- 将按钮添加到跨度
- 使用按钮添加到变量
- 单击禁用的按钮添加 CSS
- 将“下一步”和“上一个”按钮添加到较大的图像
- 将下一个和上一个按钮添加到我的图像滑块
- 按React按钮添加行
- 用jQuery点击按钮添加值
- 如何将下一个和上一个按钮添加到图像galary
- 如何为表行结果的每个按钮添加事件侦听器
- 使用setTimeout为按钮添加冷却时间
- 无法向按钮添加属性
- 如何将我的附加按钮添加到这个jQuery mp3播放器中