如何将购物车控制器数据传递给结账控制器

时间:2016-11-21 11:44:05

标签: angularjs

我需要将product_id,数量和价格传递给结帐页面我使用mysql作为后端存储数据如何存储这些数据?我开始无法做到这一点

购物车服务

   .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!'
                    });
                }
            };





             }) 

1 个答案:

答案 0 :(得分:0)

.controller('checkOutCtrl', function($scope,$sharedCartService) {

    $scope.cart=sharedCartService.cart_item; 
    $scope.cart=sharedCartService.cart;
    $scope.total_qty=sharedCartService.total_qty;
    $scope.total_amount=sharedCartService.total_amount; 
   })