-1
我使我的代码中的三个地方使用一个函数。我不喜欢重复的代码。这里是获取选定项目的预算的函数。如何清空数组,如果使用相同的函数
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
}
};
我有三个选项,第一个我选择的国家,按获取所有,并在$ scope.maxBudget我获得最大的国家所有项目的预算。如果我想要州,选定国家后,我们需要选择状态,然后按下GET ALL,并获得状态项目的最大预算...与城市相同。 问题: 上选择国家 - 最大的预算是选择状态 OK - 让所有按钮作出的国家和状态再次得到请求,并在$设置scope.maxBudget,有时预算的国家,有时为了状态。 上选择城市 - 同样喜欢STATE,在获取所有,我必须得到国家的要求,对国家和市,并设置$ scope.maxBudget有时国家,州和城市。在我的代码中,你可以看到,我尝试做空$ scope.maxBudget,但这并不奏效。
@Rajesh日Thnx的评论。你能给我更多的细节吗? Thnx – Arter
我只阅读标题并相应地评论。在我的理解中,首先你在'allSelectedb'中有冗余代码。其次,你应该使用'if .. else if ... else'而不是'if ... if ... if ...' – Rajesh