2017-02-21 20 views
-1

我有一個包含數字,用一個函數和一個用於循環我總結它裏面的所有的數字數組:求和陣列的JavaScript給我錯誤

$scope.selectedVoicesCss = []; //The array with numbers 
$scope.TotaleCss = 0; // return me the sum of numbers 

//this function populate the array 
$scope.AggiornaTotaleCss = function(param,costo,index) { 
    if(costo){ 
     $scope.selectedVoicesCss.push(param); 
     AggiornaCss(); 
    } else{ 
     $scope.selectedVoicesCss.splice(index,1);   
    } 
} 

//This function is for loop 
function AggiornaCss(){ 
    alert($scope.selectedVoicesCss); //it return me correct data! 
    for(var i = 0, n=$scope.selectedVoicesCss.length; i<n; i++) { 
     $scope.TotaleCss +=selectedVoicesCss[i]; 
    }  
} 

似乎好了,爲什麼控制檯回到我這個錯誤?: 錯誤:沒有定義selectedVoicesCss

+0

[I]的''而不是$ scope.TotaleCss + = selectedVoicesCss [I];'。你應該仔細看看這個錯誤,儘管這是不言自明的;) – briosheje

+0

哇!你是對的:D –

回答

0

變化 $scope.TotaleCss +=selectedVoicesCss[i];$scope.TotaleCss += $scope.selectedVoicesCss[i];

0
$scope.selectedVoicesCss != selectedVoicesCss 

因此就使用

$scope.selectedVoicesCss 
+0

@Rajesh感謝從手機編輯回答是一個痛苦的屁股:D – ProllyGeek

0

相反的$scope.TotaleCss +=selectedVoicesCss[i];使用$scope.TotaleCss += $scope.selectedVoicesCss[i];。你錯過了$scope

1

在這個$scope.TotaleCss +=selectedVoicesCss[i];行你缺少$scope

將其更改爲

`$ scope.TotaleCss + = $ scope.selectedVoicesCss
$scope.TotaleCss +=$scope.selectedVoicesCss[i];