i seeking achieve answer i'm sure simple, i've spent hours trying figure out matter researching , reading angularjs docs. trying increment cart item if exists within cart's array. i'm assuming use foreach method, tactics i've used have proven useless. please guide me how may achieve this? code below.
note: have attempted similar post on site, has not helped me.
controllers.js
.controller('cartctrl', function ($scope, cart) { $scope.items = cart.getitems(); $scope.getcarttotal = function () { var total = 0; (var = 0; < $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 (var = 0; < $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 }) } }; });
services.js
.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 new age', price: 459.99, spec1: '4.7-inch (diagonal) led-backlit widescreen multi-touch display ips technology', spec2: 'new 8-megapixel isight camera 1.5µ pixels', spec3: 'a8 chip 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 laptop made dell', price: 499.99, spec1: 'intel core i5-4210u 1.70 ghz turbo boost 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 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 fingerprint sensor, samsung pay , protective corning gorilla glass 4', spec3: '16 megapixel camera (2988 x 5312 pixels) + front-facing 5 megapixel camera dual-video, auto hdr, panorama, , 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(){ }]);
finally, seldom fiddles work, here image of said issue.thanks ton willing help!
you need pass addquantity function id run foreach loop in function so:
$scope.addquantity = function (id) { cartitems.foreach(function (item, index) { if(id === item.id) cartitems[index].quantity++; }); };