2017-06-24 76 views
0

HTML代碼:如何在ng bind中獲得單列值的總和?

這裏是我的HTML代碼,我綁定TOTAL每列THROUGH NG BIND作者:SUM ....

  <tr > 
       <td> Totals </td>      
       <td> {{total ||'0'}}</td>      
       <td>{{sum(user.creditTotal)}}</td> 
       <td>{{sum(user.cardTotal)}}</td> 
       <td>{{sum(user.saleAmount)}}</td> 
      </tr> 

JS:

我已經聲明範圍變量GET總計總數。任何人都可以幫助我解決我在其中犯的錯誤。 AND(數據)我在哪裏取出由API ...

$scope.total = function() { 
    $scope.cashTotal =0; 
     $scope.creditTotal =0; 
     $scope.cardTotal =0; 
    angular.forEach($scope.data, function(user) { 
     $scope.cashTotal = sum(user.cashAmount); 
     $scope.creditTotal = sum(user.creditAmount); 
      $scope.cardTotal =sum(user.cardAmount); 
     }) 

    return cashTotal,creditTotal,cardTotal; 
    } 
+0

可以顯示的$ scope.data – omer

+0

一個例子,也許這將工作,https://jsfiddle.net/suunyz3e/1414 /,我觸發了全部函數,並且還從forEach函數返回了正確的值並將其呈現在視圖中。 – omer

回答

0

我認爲這會做的伎倆。我觸發了總功能,並且還從forEach函數返回了正確的值並將其呈現在視圖中。

https://jsfiddle.net/suunyz3e/1415/

HTML:

<div ng-app="myModule"> 
    <div ng-controller="myCtrl"> 
    <tr> 
     <td> Totals </td>      
     <td>{{total}}</td>      
     <td>{{cashTotal }}</td> 
     <td>{{creditTotal}}</td> 
     <td>{{cardTotal}}</td> 
    </tr> 
    </div> 
</div> 

的JavaScript/angularjs

angular.module('myModule', ['']) 

.controller('myCtrl', function ($scope) { 
    $scope.cashTotal = 0; 
    $scope.creditTotal = 0; 
    $scope.cardTotal = 0; 

    $scope.total = function() { 
    angular.forEach($scope.data, function(user) { 
     $scope.cashTotal = sum(user.cashAmount); 
     $scope.creditTotal = sum(user.creditAmount); 
     $scope.cardTotal =sum(user.cardAmount); 
     }) 

    return $scope.cashTotal, $scope.creditTotal, $scope.cardTotal; 
    } 

    $scope.total(); 


});