如何在angular js的现有数组中添加新键? [英] How to add new key in existing array in angular js?

查看:37
本文介绍了如何在angular js的现有数组中添加新键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了空白数组 $scope.order,ON 表单提交数据获取并创建新数组,然后与现有数组合并.

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}];

当 itemDetails() 调用时,数组与新数据合并.

$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.

推荐答案

push 就地操作数组.简单

$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!

这篇关于如何在angular js的现有数组中添加新键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆