将第二个或多个数组推送到数组对象 AngularJS 中
push second or more array into array object angularjs
angular.module('print', []).
controller('Ctrl', function($scope) {
$scope.settingData = [{
"currency": "RM",
"fields": {
"type": "",
"cost": "",
"pax": "1"
}
}]
$scope.addNewFields = function() {
var row = $scope.settingData.length;
if (row < 3) {
$scope.settingData.push(row);
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div ng-app="print" ng-controller="Ctrl ">
<button ng-click="addNewFields()">Add</button>
<br />
<br />
<div class="editSection">
<div class="inputRowWrap" ng-repeat="data in settingData">
{{data.fields.type}} {{data.fields.cost}} {{data.fields.pax}}
<div class="row">
<div class="col col-40">
<label class="item item-input">
<input type="text" placeholder="Room Type #{{$index+1}}" ng-model="data.fields.type">
</label>
</div>
<div class="col col-40">
<label class="item item-input">
<input type="text" placeholder="RM" ng-model="data.fields.cost">
</label>
</div>
<div class="col">
<select ng-model="data.fields.pax">
<option>pax</option>
<option value="1">1 pax</option>
<option value="2">2 pax</option>
</select>
</div>
</div>
</div>
<div class="padding saveBtnWrap">
<button ng-click="saveSetting()">Save</button>
</div>
</div>
</div>
我管理推送以添加新的html,但新输入不起作用,我想我未能将新的数组对象添加到现有的数组对象中。
if (row < 3) {
$scope.settingData.push(row);
}
将整数推送到对象数组中。我已经更新了你的小提琴:https://jsfiddle.net/mkcegb80/1/
我们需要
推送一个对象,而不是将索引推送到数组中。
angular.module('print', []).
controller('Ctrl', function($scope) {
var data = {
"currency": "RM",
"fields": {
"type": "",
"cost": "",
"pax": "1"
}
}
$scope.settingData = [angular.copy(data)]
$scope.addNewFields = function() {
var row = $scope.settingData.length;
if (row < 3) {
// create a copy of the data object and push into the array
$scope.settingData.push(angular.copy(data));
}
}
});
相关文章:
- 将数组对象传递到struts2中的操作类
- 如何使用jquery返回php-json数组对象
- 从其名称获取javascript数组对象
- 值未与数组对象绑定
- 如何按数组/对象值的倍数过滤对象数组
- 在表中显示数组对象
- 在数组对象中分组
- 将第二个或多个数组推送到数组对象 AngularJS 中
- 重新排列 JavaScript 数组/对象
- 键上的javascript数组对象过滤器
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 用于跟踪购物车可用性的Javascript数组/对象/哈希表
- 重建有角度的java脚本数组对象
- 对JSON数组对象进行排序
- 连接与数组对象相关的文本:方式和位置
- 哪些浏览器和版本支持将常见的类数组对象直接传递到fn.apply()
- 如何使用Handlebars循环数组对象和模板
- 是否可以引用JS数组/对象中的另一个元素
- Javascript中的名称索引-数组/对象
- 数组长度不等于数组对象