我正在寻求一个答案,我确信这很简单,但我花了几个小时试图找出问题以及研究和阅读angularjs文档。我想要做的就是增加一个购物车项目,如果它已经存在于购物车的数组中。我假设我要使用forEach方法,但我使用的策略已被证明是无用的。有人可以指导我如何实现这一目标吗?代码如下。
注意:我已经尝试过本网站上的类似帖子,但它没有帮助我。
.controller('cartCtrl', function ($scope, Cart) {
$scope.items = Cart.getItems();
$scope.getCartTotal = function () {
var total = 0;
for (var i = 0; i < $scope.items.length; i++) {
var product = $scope.items[i];
total += (product.price * product.quantity);
}
var result = Math.round(total * 100) / 100;
return result;
};
$scope.getTax = function () {
var tax = 0
for (var i = 0; i < $scope.items.length; i++) {
var product = $scope.items[i];
tax += (product.price * product.quantity) * 0.075;
}
var total = Math.round(tax * 100) / 100;
return total;
};
$scope.getOrderTotal = function () {
return +parseFloat(this.getCartTotal() + this.getTax());
};
$scope.addQuantity = function () {
$scope.item.quantity++;
};
$scope.subtractQuantity = function () {
if ($scope.item.quantity > 0) {
$scope.item.quantity--;
}
};
})
.controller('detailsCtrl', function ($scope, $ionicLoading, $ionicPopup, $stateParams, Data, Cart) {
$scope.detail = Data.getItem($stateParams.productId);
$scope.addToCart = function (productId) {
var found = false;
var values = Cart.itemList;
angular.forEach(values, function (orderedItem) {
if (Cart.itemList._id == productId) {
found = true;
Cart.itemList.quantity += 1;
}
});
if (!found) {
Cart.addItem({ id: $scope.detail.id, price: $scope.detail.price, title: $scope.detail.title, img: $scope.detail.cover, quantity: 1 })
}
};
});
.service('Cart', function (Data) {
var itemList = [{
id: ''
}];
var Total = 0
return {
addItem: function(newObj){
itemList.push(newObj)
},
getItems: function () {
return itemList;
},
setItem: function (value) {
itemList = value;
},
getTotal: function () {
return Total;
},
updateItems: function(item){
this.items.push(item);
},
setTotal: function (value) {
Total = value;
}
};
})
.service('Data', function () {
var productList = [
{
title: 'iPhone 6',
cover: 'img/iphone6.png',
description: 'Apple Device of the New Age',
price: 459.99,
spec1: '4.7-inch (diagonal) LED-backlit widescreen Multi-Touch display with IPS technology',
spec2: 'New 8-megapixel iSight camera with 1.5µ pixels',
spec3: 'A8 chip with 64-bit architecture. M8 motion coprocessor',
spec4: '1080p HD video recording (30 fps or 60 fps)',
id: 0
},
{
title: 'iPhone 6 Plus',
cover: 'img/iPhone-6-Plus.png',
description: 'Apple Device of New Age 2',
price: 499.99,
spec1: '5.5" LED-backlit IPS LCD Multi-Touchscreen Shatter proof glass, oleophobic coating',
spec2: 'iOS 8, Dual-Core 1.4 GHz Cyclone (ARM v8-based) Processor, Chipset: Apple A8, PowerVR GX6650 (hexa-core graphics) Graphics',
spec3: '8 Megapixel Camera (3264 x 2448 pixels) w/ Autofocus, Dual-LED (Dual tone) Flash + Front-Facing 1.2 Megapixel Camera, 720p, burst, HDR',
spec4: 'Internal Memory: 128GB, 1GB RAM',
id: 1
},
{
title: 'iPad Air',
cover: 'img/iPadAir.png',
description: 'Apple iPad Air 2',
price: 299.99,
spec1: 'Fingerprint-resistant oleophobic coating Display',
spec2: '16 GB Flash Memory, 1 GB RAM Memory',
spec3: '10-hour battery life, 1.00 pounds',
spec4: 'Apple iOS 7; 9.7 Retina display; 2048 x 1536 resolution',
id: 2
},
{
title: 'Dell Inspiron 15.6"',
cover: 'img/dellLaptop.png',
description: 'This is a laptop made by Dell',
price: 499.99,
spec1: 'Intel Core i5-4210U 1.70 GHz Turbo Boost up to 2.70 GHz, 3MB Cache, Intel HD Graphics 5500',
spec2: '8GB PC3-12800 DDR3L 1600MHz SDRAM, 1TB 5400 rpm Hard Drive, Multiformat DVD¡ÀRW/CD-RW drive, 2 USB 2.0, 1 USB 3.0',
spec3: '15.6 in Full HD LED-backlit touchscreen with Truelife (1920 x 1080), 10-finger multi-touch support, 720p HD Webcam, HDMI',
spec4: 'Newest 802.11 AC Gigabit Wifi, 1G LAN Ethernet, Bluetooth 4.0, Waves MaxxAudio, Media Card (SD, SDHC, SDXC)',
id: 3
},
{
title: 'Galaxy S6 Edge',
cover: 'img/galaxy-s6-edge.png',
description: 'Samsung Galaxy S6 Edge',
price: 399.99,
spec1: 'Android v5.0.2 (Lollipop), Quad-Core 1.5 GHz Cortex-A53 + Quad-Core 2.1 GHz Cortex-A57 Processor, Chipset: Exynos 742, Mali-T760 Graphics',
spec2: '5.1-inch Super AMOLED Curved Edge, Multi-Touchscreen with Fingerprint sensor, Samsung Pay and Protective Corning Gorilla Glass 4',
spec3: '16 Megapixel Camera (2988 x 5312 pixels) + Front-Facing 5 Megapixel Camera with Dual-Video, Auto HDR, Panorama, and Optical Image Stabilization',
spec4: 'Internal Memory: 32GB, 3GB RAM (not expandable)',
id: 4
}
];
return {
sendData: function (data) {
productList = data;
},
getData: function () {
return productList
},
getItem: function (id) {
return productList[id];
},
addToCart: function (item) {
productList.push(item);
}
};
})
.service('BlankService', [function(){
}]);
最后,很少有我的小提琴工作,所以这里是所述问题的形象。谢谢那些愿意帮助的人!
答案 0 :(得分:0)
您需要将addQuantity函数传递给id,然后在函数中运行foreach循环,如下所示:
$scope.addQuantity = function (id) {
cartItems.forEach(function (item, index) {
if(id === item.id)
cartItems[index].quantity++;
});
};
答案 1 :(得分:0)
我能够弄明白!我分析了@JustinHerter尝试的解决方案并给了它一个简单的修改。我所要做的只是操纵我的detailsCtrl的语法,因为我甚至没有意识到当我试图调用Cart数组时,我忘了在调用函数后包含()。因为我在使用主细节模式我的“数据”服务中的本地项目,我忘了我可以使用我在addToCart函数上方插入的$ scope.detail调用当前状态的id。所以我只是能够在我的购物车数组中搜索当前状态($ Scope.detail)id,瞧,它有效!由于我在这里发现了大量类似的情况和问题,我能够解决自己的问题。片段在下面。
.controller('detailsCtrl', function ($scope, $ionicLoading, $ionicPopup, $stateParams, Data, Cart) {
$scope.detail = Data.getItem($stateParams.productId);
$scope.addToCart = function (product) {
var found = false;
var items = Cart.getCart();
angular.forEach(items, function (obj, index) {
if (obj.id == $scope.detail.id) {
items[index].quantity += 1;
found = true;
}
});
if (!found) {
Cart.addItem({ id: $scope.detail.id, price: $scope.detail.price, title: $scope.detail.title, img: $scope.detail.cover, quantity: 1 });
}
$ionicPopup.alert ({
title: 'Message',
template: 'The item was added to the cart'
})