这是我的简单购物车,我有产品清单,当我点击每个项目时,我得到详细信息,我在详细信息页面中添加到购物车按钮,将产品添加到购物车。
此问题需要立即帮助。 这是我对plunker的链接:https://plnkr.co/edit/oo05d6H6AxuJGXBAUQvr?p=preview
我还想为用户提供选择产品数量的选项,用户可以增加或减少数量?任何人都可以建议我如何实现这一点?
的script.js
var app = angular.module("myApp", ["ngRoute"]);
app.controller('mobileController', function($scope) {
$scope.items = [{
name: 'Iphone',
price: 70000,
rating: '*****',
image: 'http://i.imgur.com/hfMaGTN.png'
}, {
name: 'Oneplus',
price: 60000,
rating: '****',
image: 'http://i.imgur.com/sBcs5ZE.jpg'
}, {
name: 'Samsung',
price: 50000,
rating: '***',
image: 'http://i.imgur.com/8Bexxbf.jpg'
}, {
name: 'Sony',
price: 40000,
rating: '***',
image: 'http://i.imgur.com/0c7PcX8.png'
}, {
name: 'Moto',
price: 20000,
rating: '****',
image: 'http://i.imgur.com/HyWR1VE.png'
}];
});
app.service("cartService", [function(){
var cart = [];
function getCart(){
console.log(cart);
return cart;
}
function addToCart(item){
cart.push(item);
console.log(cart);
}
return {
getCart: getCart,
addToCart: addToCart
};
}]);
app.config(function($routeProvider) {
$routeProvider
.when("/store", {
templateUrl : "store.html",
})
.when('/item/:itemName', {
templateUrl: 'details.html',
controller: 'ItemCtrl'
})
.when("/cart", {
templateUrl: 'cartdetails.html',
controller: 'cartCtrl'
})
.when("/checkout", {
templateUrl: 'checkout.html',
controller: 'cartCtrl'
});
});
app.controller('ItemCtrl', ['$scope', '$routeParams', 'cartService',
function($scope, $routeParams, cartService) {
$scope.item = {};
angular.forEach($scope.items, function(item) {
if (item.name == $routeParams.itemName) {
$scope.item.itemName = item.name;
$scope.item.itemPrice = item.price;
$scope.item.itemRating = item.rating;
$scope.item.itemImage = item.image;
}
});
$scope.addProduct = function(item){
console.log(item);
cartService.addToCart(item);
alert(item.itemName+" added successfully") ;
};
}
]);
app.controller('cartCtrl', ['$scope','cartService',
function($scope,cartService) {
$scope.name="sasiree";
$scope.cartItems=cartService.getCart();
$scope.getTotal = function(){
var total = 0;
for(var i = 0; i < $scope.cartItems.length; i++){
var item = $scope.cartItems[i];
total += item.itemPrice ;
}
return total;
};
}
]);