以下是使用AngularJS的购物车的CRUD代码;
$scope.items = <?php echo json_encode($item_array); ?>;
$scope.cart = [];
$scope.deleteItem = function(item) {
var cart = $scope.cart;
var match = getMatchedCartItem(item);
if (match.count) {
cart.splice(cart.indexOf(item), 1);
return;
}
}
$scope.addItem = function(item) {
var match = getMatchedCartItem(item);
if (match) {
match.count += 1;
return;
}
var itemToAdd = angular.copy(item);
itemToAdd.count = 1;
$scope.cart.push(itemToAdd);
}
$scope.incQty = function(item) {
var match = getMatchedCartItem(item);
if (match) {
match.count += 1;
return;
}
}
$scope.decQty = function(item) {
var cart = $scope.cart;
var match = getMatchedCartItem(item);
if (match.count > 1) {
match.count -= 1;
return;
}
cart.splice(cart.indexOf(item), 1);
}
$scope.subTotal = function() {
var subtotal = 0;
for (var i = 0, max = $scope.cart.length; i < max; i++) {
subtotal += $scope.cart[i].price * $scope.cart[i].count;
}
$scope.subtotal = subtotal;
}
$scope.calcTotal = function() {
var total = 0;
for (var i = 0, max = $scope.cart.length; i < max; i++) {
total += $scope.cart[i].price * $scope.cart[i].count;
}
$scope.total = total + $scope.qwickCharge;
}
有了上述内容,我将使用localStorage和cookies保存所有内容。
所以用户可以返回,我只是从localStorage或cookies中检索他们的购物车。
显然,这不是一种安全的做事方式。
我想要做的是将购物车保存到MySQL数据库并使用$_COOKIE
或$_SESSION
引用它。
任何人都可以帮我解决如何嵌入角$http
以发布到我的.php
脚本服务器端。我不需要PHP代码。只是AngularJS代码,所以作为$ scope.deleteItem的一部分,例如我发布到我的数据库中删除了项目,我可以处理从我的购物车表中删除它。
FYI:item是一个包含名称,数量,价格等的数组