以下是工厂代码
LifeStyleFactoryMOdule.factory("PurchaseFactory",function(){
var productlist={products:[]};
return{
getpurchaseCart:function(){
return productlist;
},
addPurchaseCart:function(products){
productlist.products.push(products);
},
deletePurchase:function(idx){
productlist.products.splice(idx,1)
}
}
})
服务
LifeStyleServiceModule.service("PurchaseService",function(PurchaseFactory){
this.getAllPurchase=function(){
return PurchaseFactory.getpurchaseCart();
}
this.addPurchase=function(products)
{
PurchaseFactory.addPurchaseCart(products);
}
this.deletePurchase=function(idx,id)
{
PurchaseFactory.deletePurchase(idx,id);
}
})
具有功能的控制器
LifeStyleController.controller("PurchaseController",function($scopePurchaseService){
$scope.savepurchase=function(products){
if($scope.products._id==undefined){
$scope.products=angular.extend($scope.products,$scope.sizes)
PurchaseService.addPurchase($scope.products);
$scope.products={};
$scope.sizes={size:[]}
}
}
在HTML中我有一个按钮和我发送的所有数据。 第一次我推送数据是成功完成,但第二次它显示我错误,因为productlist.products.push不是一个函数
知道答案将是很有帮助的