如何在现有的角JS阵列添加新的关键? [英] How to add new key in existing array in angular js?

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

问题描述

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

  .controller('GuestDetailsCtrl',函数($范围){
    $ scope.order = [];
    $ scope.itemDetails =功能(){
        //这里得到的数据形式submiti后有阵列从形式arrieve提交。
        $ scope.order = $ scope.order.push({名:$ scope.item.name,价格:$ scope.item.price});
    }
});

我想造成这个样子。结果


  

$ scope.order = [{名称:'ABC',价格:100},{名称:'焊接工艺评定,价格:80},{名称:'某某',价格:50}];结果
  结果
  当itemDetails()当时阵通话用新的数据进行合并。



解决方案

就地阵列上运行。简单

  $ scope.order = [];
$ scope.itemDetails =功能(){
    //这里得到的数据形式submiti后有阵列从形式arrieve提交。
    $ scope.order.push({名:$ scope.item.name,价格:$ scope.item.price});
}

(不分配的话),而应该努力!

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.

解决方案

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!

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

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