2016-11-21 7 views
0

にカートのコントローラデータを渡すためには、どのように私はbegineerはそれを行うことができないのですか?私は、これらのデータを格納する方法、データを格納するためのバックエンドとしてMySQLを使用していますチェックアウトページにPRODUCT_ID、数量と価格を渡す必要があり、チェックアウトコントローラ

カートサービス

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





      }) 
+0

このサービスを使用すると、コントローラ間でデータを交換できます。 – sreeramu

+0

いくつかの例を挙げることができますか?私は全く新しいです。 – user3698911

+0

この例は、http://stackoverflow.com/a/40690771/3279156で確認できます。 – sreeramu

答えて

0
にその製品の詳細情報を渡す必要がカートに追加されたカートコントローラ
関連する問題