使用其他控制器计算 ng-repeat 中的总行数 [英] Calculate total of rows in ng-repeat with other controller

查看:27
本文介绍了使用其他控制器计算 ng-repeat 中的总行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了要计算总数的行.

I got rows where a total will be calculated.

在我的 ng-repeat 中,我有一个控制器 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-repeat 中的总行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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