0
 $scope.updateCart = function(item) {
                var index;
                var items = 0;
                var cost = 0;
                if (item.ref) {
                    $scope.selectedMenueItems.push(item);
                    console.log('updatedd item ' + JSON.stringify($scope.selectedMenueItems));
                } 
}

 $scope.addItem = function(item) {
                console.log('item clicked is ' + JSON.stringify(item));
                var temp = item;
                temp.quantity = 0;
                if (item.item_details.item_sub_category.length > 0) {
                    var itemDetails = item;
                    var modalInstance = $modal.open({
                        backdrop: 'static',
                        keyboard: false,
                        templateUrl: 'template/itemOptions.html',
                        controller: 'itemOptionsController',
                        resolve: {
                            itemDetails: function() {
                                return itemDetails;
                            }
                        }

                    });

                    modalInstance.result.then(
                        function(result) {
                            if (result.length > 0) {
                                temp.totalPrice = temp.originalCost;
                                temp.ref = [];
                                angular.forEach(result, function(info) {
                                    if (!info.option_cumpulsory) {
                                        temp.totalPrice += info.option_price;
                                    }
                                    var item = {};
                                    item.id = info._id;
                                    item.name = info.option_name;
                                    item.quantity = info.quantity;
                                    item.option_cumpulsory = info.option_cumpulsory;
                                    item.price = info.option_price;
                                    temp.ref.push(item);
                                });
                                temp.quantity += 1;
                                console.log('item to be added ' + JSON.stringify(temp));
                                $scope.updateCart(temp);
                            } else {
                                temp.quantity = temp.quantity + 1;
                                $scope.updateCart(temp);
                            }
                        },
                        function(result) {
                            if (!result) {
                                temp.quantity = temp.quantity + 1;
                                $scope.updateCart(temp);
                            }
                        }
                    );
                }
}

Here inside item.ref when I push it for the first time, it works fine.but when I push it for the second time with different ref options. It pushes two items , but ref items are copied for both the items from last pushed item.How to have duplicate items with different refs?

1 Answer 1

1

Use angular.copy to break reference. It must be passing by reference...

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.