2

I am trying to add a list of products and upsell for these products (1 product may be added multiple times to cart). Using addProduct function i am adding product from available list to an array like this

$scope.addToCartData.push($scope.addproduct[id]);

each product further has an array of upsell products, now in another controller in which i have logic for upsell products, i am incrementing upsell product qty using the following code

$scope.addUpsellPro=function(pid,pos)
        {
          angular.element(document.querySelector('[ng-controller="ManagerCtrl"]')).scope().addToCartData[pos].upsellsProducts[pid].qty++;
          console.log(pos); 
        }

in here pid is the upsell product id and pos is the index value taken from

addToCartData

but the strange thing happen is it increments qty of upsell products for all same products. e.g. if I have a product PRO1 in addproduct array, this product have up1 and up2 as upsells, now if I push PRO1 two times to the addToCartData array and then tries to increment qty of upsell product of PRO1 at postion 0 (first). it also update upsell products qty at postion 1 :(

Image for duplicate upsells enter image description here

10
  • I would suggest keeping that kind of data in a service than in controllers. That way controllers in various parts of an app can call the central service to get the data, it's in one place and more manageable. Commented May 31, 2016 at 14:37
  • @rrd thanks for your suggestion, but I am absolutely new to angularjs and don't know a lot about it (services). can you point out something wrong in the given code? Commented May 31, 2016 at 14:43
  • 1
    @Haris The way you are using angular is not the best (no hard feelings :)) A must read for angular developers: github.com/johnpapa/angular-styleguide Commented May 31, 2016 at 14:51
  • @RonDadon is right, that's a good guide to follow Commented May 31, 2016 at 14:56
  • Why do you say "it also update upsell products qty at postion 1"? Where do you see that? Commented May 31, 2016 at 16:00

1 Answer 1

1

I got it to work, in my limited knowledge of angularjs, pushing array index into another array doesn't copy the array/object but assign a reference therefore when i was updating product qty on base of position it was actually updating value of addproduct array, not of addToCartData. so what i did to tackle this is I used angular.copy() function and changed my code

$scope.addToCartData.push($scope.addproduct[id]);

to

$scope.addToCartData.push($scope.assignProduct(id));

and then created assignproduct funciton like;

$scope.assignProduct= function(pid)
{
  var curr_product=[];
  angular.copy($scope.addproduct[pid], curr_product);
  return curr_product;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.