angular无法推送数组

时间:2017-11-23 18:43:49

标签: javascript angularjs

 $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);
                            }
                        }
                    );
                }
}

这里面是item.ref,当我第一次推它时,它运行正常。但是当我第二次用不同的ref选项推它时。它会推送两个项目,但会为上一个推送项目中的两个项目复制参考项目。如何使用不同refs的重复项目?

1 个答案:

答案 0 :(得分:1)

使用angular.copy打破引用。它必须通过引用传递......