如何获取ng bind中单个列值的总和..?

时间:2017-06-24 07:42:00

标签: html angularjs-scope

HTML CODE:

这是我的HTML代码,在那里我通过NG绑定各个列的总和......

            <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:

我已经宣布了范围变量以获得总和。可以任何人帮助我在我的错误。 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;
    }

1 个答案:

答案 0 :(得分: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();


});