angularjs将项添加到对象数组

时间:2015-09-22 04:29:33

标签: arrays angularjs object

我有一组看起来像这样的数组和对象

`

var PaymentContent = "Payments":
     [{
        "Details": {
                    "PaymentType": "CreditCard",
                    "Amount": $scope.total,
                    "CCNAME": $scope.Form.CreditCard.FullName,
                      }
      }]
Payments: Array[1]
0:Object
 Details: Object
 Amount: 5.99
 CCNAME: null
 PaymentType: "CreditCard"`

现在,我如何使用angularjs更新那组对象和数组? 期望的输出:

Payments: Array[1] 0:Object Details: Object Amount: 5.99 CCNAME: null PaymentType: "CreditCard" LastPayment: "04/11/2011"

注意lastpayment字段。

这是我的代码

var paymentDetails = {LastPayment : '04/11/2011', LastSignOn : '04/11/2011'} fields = angular.extend({}, PaymentContent , paymentDetails);

谢谢!

2 个答案:

答案 0 :(得分:2)

您正在将空对象设置为目标。这个新对象将接收其他2个对象的属性和值...而不更改其他2个源对象

如果希望数组对象接收更新,请删除第一个参数(空对象)

angular.extend( fields.Payments[0].Details, paymentDetails);

这将使用fields.Payments[0].Details

中的所有属性和值更新paymentDetails

Demo Fiddle

答案 1 :(得分:2)

您可以直接写下面的代码:

Payments[0].LastPayment = "04/11/2011";