0
我需要傳遞的product_id,數量和價格來結賬頁面我使用MySQL作爲後端存儲數據如何存儲這些數據嗎?我是begineer不能夠做到這一點如何到購物車控制器數據傳遞到結賬控制器
車服務
.factory('sharedCartService', ['$ionicPopup',function($ionicPopup){
var cartObj = {};
cartObj.cart=[];
cartObj.total_amount=0;
cartObj.total_qty=0;
cartObj.cart.add=function(id,image,name,price,qty){
if(cartObj.cart.find(id)!=-1){
var alertPopup = $ionicPopup.alert({
title: 'Product Already Added',
template: 'Increase the qty from the cart'
});
//cartObj.cart[cartObj.cart.find(id)].cart_item_qty+=1;
//cartObj.total_qty+= 1;
//cartObj.total_amount+= parseInt(cartObj.cart[cartObj.cart.find(id)].cart_item_price);
}
else{
cartObj.cart.push({ "cart_item_id": id , "cart_item_image": image , "cart_item_name": name , "cart_item_price": price , "cart_item_qty": qty });
cartObj.total_qty+=1;
cartObj.total_amount+=parseInt(price);
}
};
cartObj.cart.find=function(id){
var result=-1;
for(var i = 0, len = cartObj.cart.length; i < len; i++) {
if(cartObj.cart[i].cart_item_id === id) {
result = i;
break;
}
}
return result;
};
cartObj.cart.drop=function(id){
var temp=cartObj.cart[cartObj.cart.find(id)];
cartObj.total_qty-= parseInt(temp.cart_item_qty);
cartObj.total_amount-=(parseInt(temp.cart_item_qty) * parseInt(temp.cart_item_price));
cartObj.cart.splice(cartObj.cart.find(id), 1);
};
cartObj.cart.increment=function(id){
cartObj.cart[cartObj.cart.find(id)].cart_item_qty+=1;
cartObj.total_qty+= 1;
cartObj.total_amount+=(parseInt(cartObj.cart[cartObj.cart.find(id)].cart_item_price));
};
cartObj.cart.decrement=function(id){
cartObj.total_qty-= 1;
cartObj.total_amount-= parseInt(cartObj.cart[cartObj.cart.find(id)].cart_item_price) ;
if(cartObj.cart[cartObj.cart.find(id)].cart_item_qty == 1){ // if the cart item was only 1 in qty
cartObj.cart.splice(cartObj.cart.find(id) , 1); //edited
}else{
cartObj.cart[cartObj.cart.find(id)].cart_item_qty-=1;
}
};
return cartObj;
}])
車控制 當一個產品被添加到購物車我需要通過該產品的詳細信息結帳控制器
.controller('cartCtrl', function($scope,$rootScope,sharedCartService,$ionicPopup,$state,$http) {
$scope.cart=sharedCartService.cart_item; // Loads users cart
//onload event-- to set the values
$scope.$on('$stateChangeSuccess', function() {
$scope.cart=sharedCartService.cart;
$scope.total_qty=sharedCartService.total_qty;
$scope.total_amount=sharedCartService.total_amount;
});
//remove function
$scope.removeFromCart=function(c_id){
$scope.cart.drop(c_id);
$scope.total_qty=sharedCartService.total_qty;
$scope.total_amount=sharedCartService.total_amount;
};
$scope.inc=function(c_id){
$scope.cart.increment(c_id);
$scope.total_qty=sharedCartService.total_qty;
$scope.total_amount=sharedCartService.total_amount;
};
$scope.dec=function(c_id){
$scope.cart.decrement(c_id);
$scope.total_qty=sharedCartService.total_qty;
$scope.total_amount=sharedCartService.total_amount;
};
$scope.proceed=function(){
if($scope.total_amount>0){
$state.go('checkOut');
}
else{
var alertPopup = $ionicPopup.alert({
title: 'No item in your Cart',
template: 'Please add Some Items!'
});
}
};
})
您可以使用該服務在控制器之間交換數據。 – sreeramu
你可以提供一些例子,我完全是這個新的 – user3698911
你可以檢查這個例子,http://stackoverflow.com/a/40690771/3279156,它可能會幫助你理解。 – sreeramu