如何在angularJs中进行算法计算并保存下一行的结果?

时间:2022-03-02 19:45:17

I have one table and I want to do add/subtraction in each row and preserve the result for next row.

我有一个表,我想在每一行中做加减法,并保存下一行的结果。

<tr ng-repeat="statement in statements.result.data">
   <td>{{statement|getClosingBalance:statements.opening_balance|number:2}}</td>
</tr>

Filter,

过滤器,

invoiceApp.filter('getClosingBalance', function () {
return function (statement, balance) {
    if (statement.type == 1) {
        return parseFloat(balance) + parseFloat(statement.amount);
    } else if (statement.type == 2) {
        return parseFloat(balance) - parseFloat(statement.amount);
    }
}
});

It is working fine as I written code, but I want to preserve balance after each and every operations.

它在编写代码时运行良好,但我希望在每次操作之后保持平衡。

How can I implement the same?

我如何实现它呢?

2 个解决方案

#1


2  

why not calculate the data in a service, and keep your view clean. calculate the data in service then just render the data directly, that make your view clean and service easy to be tested.

为什么不计算服务中的数据,保持视图的清洁。计算服务中的数据,然后直接呈现数据,使您的视图清晰,服务易于测试。

   app.factory('openingBalanceService', function() {

    function calculateOpeningBalance(data){
       var result = angular.copy(data);
       /*do calculation on result*/
       return result
    }

    return{
      calculateOpeningBalance: calculateOpeningBalance
    };

   });

#2


0  

I am doing all calculation in controller itself when data received from db or api,

当从db或api接收数据时,我在控制器中进行所有的计算,

$scope.statements.balance = $scope.statements.opening_balance;
                    angular.forEach($scope.statements.result.data, function (obj) {
                        if (obj.type == 1) {
                            obj.closing_balance = parseFloat($scope.statements.balance) + parseFloat(obj.amount)
                        } else if (obj.type == 2) {
                            obj.closing_balance = parseFloat($scope.statements.balance) - parseFloat(obj.amount)
                        }
                        $scope.statements.balance = obj.closing_balance;
                    });

Html,

Html,

<td class="text-right" data-ng-bind="(company.currency)+' '+(statement.closing_balance|number:2)"></td>

So there will not be any kind of calcuaiton in html or inside the loop :)

所以在html或循环中不会有任何计算方法

#1


2  

why not calculate the data in a service, and keep your view clean. calculate the data in service then just render the data directly, that make your view clean and service easy to be tested.

为什么不计算服务中的数据,保持视图的清洁。计算服务中的数据,然后直接呈现数据,使您的视图清晰,服务易于测试。

   app.factory('openingBalanceService', function() {

    function calculateOpeningBalance(data){
       var result = angular.copy(data);
       /*do calculation on result*/
       return result
    }

    return{
      calculateOpeningBalance: calculateOpeningBalance
    };

   });

#2


0  

I am doing all calculation in controller itself when data received from db or api,

当从db或api接收数据时,我在控制器中进行所有的计算,

$scope.statements.balance = $scope.statements.opening_balance;
                    angular.forEach($scope.statements.result.data, function (obj) {
                        if (obj.type == 1) {
                            obj.closing_balance = parseFloat($scope.statements.balance) + parseFloat(obj.amount)
                        } else if (obj.type == 2) {
                            obj.closing_balance = parseFloat($scope.statements.balance) - parseFloat(obj.amount)
                        }
                        $scope.statements.balance = obj.closing_balance;
                    });

Html,

Html,

<td class="text-right" data-ng-bind="(company.currency)+' '+(statement.closing_balance|number:2)"></td>

So there will not be any kind of calcuaiton in html or inside the loop :)

所以在html或循环中不会有任何计算方法