如何将命名的Json数组推入Json对象

时间:2016-03-27 07:39:42

标签: angularjs

我想推送下面的数据。我想订购商品,它包含以下字段:

var OrderItem = {"itemid":item.itemid,"itemname":item.itemname,"qty":1,"itemprice":item.itemprice};

但我想像这样存储

{
    "Items": [
        {
            "itemid": 50,
            "itemname": "sample",
            "itemprice": 124,
            "qty": 1
        },
        {
            "itemid": 52,
            "itemname": "sample",
            "itemprice": 124,
            "qty": 1
        }
    ]
}

控制器:

 $scope.addnew=function(item){        
  var OrderItem= {"itemid":item.itemid,"itemname":item.itemname,"qty":1,"itemprice":item.itemprice};     
        NewOrderService.addOrderitems(OrderItem);
    }

}

服务:

servctrl.service("NewOrderService", function(NewOrderFactory) {

    this.addOrderitems = function(orderitemnew) {
         NewOrderFactory.AddOrdernew(orderitemnew);
    }  
});

,厂区:

factmodule.factory("NewOrderFactory",function(){

  var orderitemsnew=[];

  return {
     AddOrdernew:function(orderitemnew){
        orderitemsnew.push(orderitemnew);
     }  
   }
});

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

可能是这样的?

factmodule.factory("NewOrderFactory",function(){

    var orderitemsnew={
        items = [];
    };

    return{
        AddOrdernew:function(orderitemnew){
            orderitemsnew.items.push(orderitemnew);
        },
        getOrderItemsNewList: function() {
            return orderitemsnew;
        }
    };
});