2017-04-04 10 views
0

私はAngular JSサービスを使用してカートを作成しようとしています。このサービスはUserCartと呼ばれます。 UserCartサービスは2つの変数で構成されています.1つはカートの合計金額を維持し、もう1つはカートに追加されたアイテムを格納する変数です。さらに、カート内のアイテムを追加または削除する2つの機能があります。Angular JSでサービスが動作しないデータバインド

UserCartサービスは、2つの機能(商品の追加と削除)とカートの合計金額を持つオブジェクトを返します。

View(html code at end)には、それぞれの製品(現在はシャツとワイン)の「カートに入れる」ボタンがあります。クリックすると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

total_cart_valは原始的であるので、あなたはそれを代入しているときcart.cart_val、あなたは値とnを割り当てている変数への参照。 cart.cart_valプロパティではなくtotal_cart_val変数を更新しているため、変更はエクスポートされたカートオブジェクトに反映されず、その結果ビューに反映されます。

+0

Thiattの魅力のように機能します!ありがとう – Ouroboros

1

$scope.userCart = UserCart.cart;を変更してみてください:ここに私のIRCの答えを投稿

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

しかし、これがなぜ必要なのですか?私がやっていることがうまくいかない理由は何ですか?そのすべての慣習を本当に念頭に置いています。リンクは理解される – Ouroboros

+0

変更が機能しませんでした。あなたができるなら実行可能なコードを提供してもらえますか? – Ouroboros

関連する問題