sum数组javascript给我错误

时间:2017-02-21 12:01:32

标签: javascript angularjs sum

我有一个包含数字的数组,有一个函数和一个for循环我总结了它里面的所有数字:

$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

4 个答案:

答案 0 :(得分:1)

在此$scope.TotaleCss +=selectedVoicesCss[i];行中,您缺少$scope

将其更改为

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

答案 1 :(得分:0)

更改 $scope.TotaleCss +=selectedVoicesCss[i];$scope.TotaleCss += $scope.selectedVoicesCss[i];

答案 2 :(得分:0)

$scope.selectedVoicesCss != selectedVoicesCss

所以只需使用

$scope.selectedVoicesCss

答案 3 :(得分:0)

而不是$scope.TotaleCss +=selectedVoicesCss[i];使用$scope.TotaleCss += $scope.selectedVoicesCss[i];。你错过了$scope

相关问题