使用不适用于Angular JS的服务的数据绑定

时间:2017-04-04 19:13:33

标签: javascript angularjs angular-services

我尝试使用Angular JS Service创建购物车。此服务称为UserCart。 UserCart服务包含两个变量,一个用于维护购物车的总价值,另一个用于存储购物车中添加的商品。它还包含两个添加或删除购物车中项目的功能。

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

在视图(结尾的html代码)中,有“添加到购物车”'每个产品(当前是衬衫和葡萄酒)的按钮,点击后应该调用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
        };
    });
}());
&#13;
&#13;
&#13;

我的ProductController.js就像这样

&#13;
&#13;
(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);

}());
&#13;
&#13;
&#13;

我的观点如下。

&#13;
&#13;
    <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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

尝试将$scope.userCart = UserCart.cart;更改为:

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

答案 1 :(得分:1)

在此处发布我的IRC答案:

total_cart_val是原语,因此当您将其分配给cart.cart_val时,您将分配值而不是对变量的引用。由于您要更新total_cart_val变量而不是cart.cart_val属性,因此更改不会反映在导出的购物车对象中,因此会反映在您的视图中。