私は商品のリストを持っている私のシンプルなショッピングカートです。各アイテムをクリックすると、詳細が表示され、カートに商品を追加する詳細ページのカートボタンに追加しました。カートの商品数量を角度で選択するオプションをユーザに提供したいですか?
を参照してくださいしかし、私は、製品の数量を選択するオプションをユーザーに提供したい、ユーザーが数量を増減することができますか?また、カート内のアイテムの数に制限があるはずです!誰も私がこれを実装する方法を提案することはできますか?
誰かが、コードに追加する効率を、ユーザーにとって使いやすく、構造化された方法で提案すると本当に役に立ちますか?
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="saisree";
$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;
};
}
]);
あなたのロジックを理解しましたが、ユーザーが製品の数量を与える方法がありますか?それはcartdetailsページに反映されるはずです。例えば、iPhoneの数量として2を、カートの詳細ページで2追加されますか? – vennapusa
ここに行く:https://plnkr.co/edit/if89lOuYxHY7OYBRPG9q?p=preview – robert