如何使用相同的函数清空数组

时间:2016-10-25 08:20:45

标签: javascript angularjs

我在我的代码中的三个地方使用了一个函数。我不喜欢重复代码。这是获取所选项目预算的功能。

var getBudgetForAll = function(){
                            var bId = [];
                                        angular.forEach($scope.myCartItems, function (value, key) {
                                            bId.push(value.id);
                                        });
                                        $http.get(serviceBase + 'xxxx/' + $rootScope.user_id + '/xxxxx/calculate/max-budget/from/' +
                                                $scope.ctrl.picker4.date + '/to/' + $scope.ctrl.picker5.date + '?smartb=' + benchId.join())
                                                .success(function (maxBudget)
                                                {
                                                    $scope.maxBudget = maxBudget;

                                                });

                        }

现在,我在代码

中的三个位置使用此功能
 $scope.allSelectedb = function () {
                             var onebyOneAddedB = getOnebyOneAddedB($scope.myCartItems);
                                $scope.myCartItems = [];
                                $scope.maxBudget = []; //here i try empty maxBudget
                            if ($scope.filteredCountries !== undefined) {
                                $scope.maxBudget = []; //here i try empty
                                $scope.myCartItems = [];
                                 addNonDuplicateItems($scope.filteredCountries); 
                                 addNonDuplicateItems(onebyOneAddedB);
                                 getBudgetForAll(); //here i call function

                            }
                            if ($scope.filteredStates !== undefined) {
                                $scope.maxBudget = []; //here i try empty
                                $scope.myCartItems = [];
                                $scope.myCartItems = $scope.filteredStates;
                                addNonDuplicateItems($scope.filteredStates);
                                addNonDuplicateItems(onebyOneAddedB);
                                getBudgetForAll(); //here i call function
                            }
                            if ($scope.filteredCities !== undefined) {
                                $scope.maxBudget = []; //here i try empty
                                $scope.myCartItems = [];
                                addNonDuplicateItems($scope.filteredCities);
                                addNonDuplicateItems(onebyOneAddedB);
                                getBudgetForAll(); //here i call function
                            }
                        };

我有三个选项,首先我选择国家/地区,然后按GET ALL,并在$ scope.maxBudget中获取国家/地区中所有项目的最高预算。如果我想要州,在选定的国家之后,我们需要选择州,然后按下全部,并获得州内物品的最大预算...对于城市而言相同。 问题: 选择COUNTRY - 最高预算即可 on select STATE - GET ALL按钮再次为国家和州提出请求,并设置 $ scope.maxBudget ,有时为国家预算,有时为州预算。 on select CITY - 与 STATE 相同,在GET ALL上,我收到COUNTRY,STATE和CITY的请求,并设置 $ scope.maxBudget 有时为国家,为州和为城市。在我的代码中,你可以看到,我尝试空 $ scope.maxBudget ,但这不起作用。

1 个答案:

答案 0 :(得分:0)

thnx @rajesh的提示。我很容易解决这个问题,我只需要把

  

getBudgetForAll();

从if :)