问题
I have define blank array $scope.order, ON form submit data get and create new array then merge with existing array.
.controller('GuestDetailsCtrl',function($scope){
$scope.order = [];
$scope.itemDetails = function() {
// Here data get after form submiti have array arrieve from form submit.
$scope.order=$scope.order.push({name:$scope.item.name,price:$scope.item.price});
}
});
I want result like this.
$scope.order = [{name:'abc',price:100},{name:'pqr',price:80},{name:'xyz',price:50}];
When itemDetails() call at that time array merge with new data.
回答1:
push operates on the array in-place. Simply
$scope.order = [];
$scope.itemDetails = function() {
// Here data get after form submiti have array arrieve from form submit.
$scope.order.push({name:$scope.item.name,price:$scope.item.price});
}
(without assigning it), and that should work!
来源:https://stackoverflow.com/questions/30635512/how-to-add-new-key-in-existing-array-in-angular-js