我正在嘗試使用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> </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> </span>
</div>
</div>
</section>
作品像一個魅力Thiatt!謝謝 – Ouroboros