计算总排在NG-重复与其他控制器 [英] Calculate total of rows in ng-repeat with other controller

查看:270
本文介绍了计算总排在NG-重复与其他控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里一共将计算行。

I got rows where a total will be calculated.

在我的NG-重复我有一个控制器 RowCtrl ,我计算出总的。

In my ng-repeat I have a controller RowCtrl where I calculate the total.

app.controller('RowCtrl', function ($scope) {
   $scope.unit_price = 0;
   $scope.quantity = 0;

   $scope.$watchCollection('[row.unit_price, row.quantity]', function () {
      $scope.row.total = $scope.row.unit_price * $scope.row.quantity;
   });
});

我如何计算总的所有行?

How can I calculate the total of all the rows?

在fiddlr我有其中总一排已经计算的行的演示。

In the fiddlr I have a demo of the rows where the total of a row is already calculated.

Fiddlr: http://jsfiddle.net/9t9em/1/

推荐答案

您不需要RowCtrl

you don't need RowCtrl

http://jsfiddle.net/9t9em/5/

app.controller('MainCtrl', function ($scope) {
    $scope.rows = [{
        unit_price: 1,
        quantity: 0
    }, {
        unit_price: 2,
        quantity: 0
    }, {
        unit_price: 3,
        quantity: 0
    }];
    $scope.$watch('rows', function () {
        $scope.total = 0
        angular.forEach($scope.rows, function(row){
            $scope.total += row.unit_price * row.quantity
        })
    }, true)
});

这篇关于计算总排在NG-重复与其他控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆