2017-04-04 33 views
0

我正在尝试使用Angular JS服务创建购物车。这项服务称为UserCart。 UserCart服务由两个变量组成,一个用于维护购物车的总价值,另一个用于存储购物车中添加的物品。它另外包含两个功能来添加或删除购物车中的物品。使用服务不适用于Angular JS的数据绑定

UserCart服务返回一个具有两个功能(添加/删除产品)和总购物车值的对象。

在视图(html代码结尾)中,每个产品(当前是衬衫和酒)都有'Add to Cart'按钮,点击时应该调用ProductController的函数'addToCart',该函数应该然后更新UserCart的total_cart_val(并且这样做是因为我从控制台日志中确认)。但是,这个值并不反映在HTML中。

是否有任何明显的代码问题?下面

UserCart.js:

(function() { 
 
    // Get reference to the app 
 
    var app = angular.module("jargoViewer"); 
 

 
    // Create the factory that share the User Cart with various controllers 
 
    app.factory('UserCart', function(){ 
 
     var cart_items = []; 
 
     var total_cart_val = 0; 
 
     
 
     var addProductInCart = function(prodID, prodCostPerUnit, prodQuantity) { 
 
      if((prodID in cart_items)) { 
 
       // true if "prodID" exist in cart_items 
 
       // Add the new prodID key element now 
 
       prodObj = cart_items[prodID]; 
 
       prodObj.Quantity = prodObj.Quantity + prodQuantity; 
 
      } else { 
 
       // A product with same key already exists 
 
       cart_items[prodID] = { 
 
        'Quantity' : prodQuantity, 
 
        'CostPerUnit' : prodCostPerUnit 
 
       }; 
 
      } 
 
      // Add the total newly added products cost to Total Cart Value 
 
      total_cart_val += prodCostPerUnit * prodQuantity; 
 
     } 
 
     
 
     var removeProductInCart = function(prodID, prodQuantity) { 
 
      if((prodID in cart_items)) { 
 
       // true if "prodID" exist in cart_items 
 
       // Add the new prodID key element now 
 
       prodObj = cart_items[prodID]; 
 
       existingQuantity = prodObj.Quantity; 
 
       if(prodQuantity > existingQuantity) { 
 
        alert('No more of this item exists in the cart!'); 
 
       } else { 
 
        prodObj.Quantity = prodObj.Quantity - prodQuantity; 
 
        // Add the total newly added products cost to Total Cart Value 
 
        total_cart_val -= prodCostPerUnit * prodQuantity; 
 
        if(prodObj.Quantity < 1) { 
 
         // No more of this product left in cart, remove from cart list 
 
         cart_items.splice(prodID, 1); 
 
        } 
 
       } 
 
      } else { 
 
       // Error 
 
       alert('No more of this item exists in the cart!'); 
 
      } 
 
     } 
 
     
 
     // Return the Interface of UserCart 
 
     return { 
 
      // products_in_cart: cart_items, 
 
      cart : { 
 
       cart_val : total_cart_val 
 
      }, 
 
      addProdInCart : addProductInCart, 
 
      delProdInCart : removeProductInCart 
 
     }; 
 
    }); 
 
}());

我ProductController.js是这样

(function() { 
 

 
    var app = angular.module("jargoViewer"); 
 

 
    //var ProductController = function($scope) { 
 
    var ProductController = function($scope, UserCart) { 
 

 
     $scope.addToCart = function(prodID, costPerUnit, quantity) { 
 
      UserCart.addProdInCart(prodID, costPerUnit, quantity); 
 
     } 
 
     
 
     $scope.products = [ 
 
      { 
 
       'title' : 'First Product', 
 
       'id' : 100001, 
 
       'img' : 'product/shirt.jpg', 
 
       'cost' : 899, 
 
       'sizes' : [ 
 
        { 
 
         'label' :'Small' 
 
        }, 
 
        { 
 
         'label' :'Medium' 
 
        } 
 
       ] 
 
      }, 
 
      { 
 
       'title' : 'Second Product', 
 
       'img' : 'product/wine.png', 
 
       'id' : 100002, 
 
       'cost' : 3150, 
 
       'sizes' : [ 
 
        { 
 
         'label' :'Small' 
 
        }, 
 
        { 
 
         'label' :'Large' 
 
        } 
 
       ] 
 
      } 
 
     ]; 
 
     
 
     $scope.userCart = UserCart.cart; 
 
    }; 
 
    
 
    app.controller("ProductController", ProductController); 
 

 
}());

我的观点是如下。

<section class="big-product"> 
 
     <div class="container"> 
 
      <div class="row top30" ng-repeat="prod in products"> 
 
       <span>&nbsp</span> 
 
       <div> 
 
        <div class="col-sm-4 product"> 
 
         <!--<img src="product/shirt.jpg" alt="t-shirt" class="img-responsive">--> 
 
         <img src="{{prod.img}}" alt="t-shirt" class="img-responsive"> 
 
        </div> 
 
        <div class="col-sm-8 info"> 
 
         <div class="info-wrapper" > 
 
          <h2>{{prod.title}}</h2> 
 
          <p class="lead"> 
 
           {{prod.desc}} 
 
          </p> 
 

 
          <ul class="list-inline"> 
 
           <li> 
 
             <div class="dropdown"> 
 
              <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown"> 
 
               Size 
 
               <span class="caret"></span> 
 
              </button> 
 
              <ul class="dropdown-menu" role="menu" aria-labelledby="menu1"> 
 
                <li role="presentation" ng-repeat="prop in prod.sizes"><a role="menuitem" tabindex="-1" href="#">{{prop.label}}</a></li> 
 
                <!--<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Medium</a></li> 
 
                <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Large</a></li>--> 
 
              </ul> 
 
             </div> 
 
           </li> 
 
           <li> 
 
             <div class="dropdown"> 
 
              <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown"> 
 
               Quantity 
 
               <span class="caret"></span> 
 
              </button> 
 
              <ul class="dropdown-menu" role="menu" aria-labelledby="menu1"> 
 
                <li role="presentation"><a role="menuitem" tabindex="-1" href="#">1</a></li> 
 
                <li role="presentation"><a role="menuitem" tabindex="-1" href="#">2</a></li> 
 
                <li role="presentation"><a role="menuitem" tabindex="-1" href="#">3</a></li> 
 
              </ul> 
 
             </div> 
 

 
           </li> 
 
           <li class="currency"> 
 
            {{prod.cost}} 
 
           </li> 
 
           <li class="Total Cart Value"> 
 
            {{userCart.cart_val}} 
 
           </li> 
 
          </ul> 
 
         </div> 
 
         <a class="btn btn-success btn-unique" ng-click = "addToCart(prod.id, prod.cost, 1)">Add to Cart<i class="icon-cart-1"></i></a> 
 
        </div> 
 
       </div> 
 
       <span>&nbsp</span> 
 
      </div> 
 
     </div> 
 
    </section>

回答

1

张贴在这里我的IRC答案:

total_cart_val是一种原始的,所以当你将其分配给cart.cart_val,您正在分配值和n对变量的引用。由于您正在更新total_cart_val变量而不是cart.cart_val属性,所以这些更改不会反映在导出的购物车对象中,因此也不会反映在您的视图中。

+0

作品像一个魅力Thiatt!谢谢 – Ouroboros

1

尝试改变$scope.userCart = UserCart.cart;到:

$scope.userCart= function() { 
    return UserCart.cart; 
}; 
+0

好的,但为什么这需要?任何我为什么做的原因是行不通的?它确实介意所有这些惯例。链接将不胜感激 – Ouroboros

+0

变化没有奏效。如果可以,你能否提供可行的代码? – Ouroboros