如何使用angularjs查找多行总数

时间:2019-07-08 07:35:28

标签: php angularjs

我试图找到总数,但显示为0。我正在localhost xamp服务器上尝试。

<div data-ng-app="myapp" data-ng-controller="CartForm">
  <table>
    <tr ng-repeat="x in names">
      <td>{{ x.price }}</td>
      <td><input type="number" ng:model="x.qty" /></td>
      <td>{{ x.qty * x.price | currency: "Rs " }}</td>
    </tr>
    <tr>
      <td>Total:</td>
      <td>{{ total() }}</td>
    </tr>
  </table>
</div>

2 个答案:

答案 0 :(得分:0)

ng-model的语法约定不好,但是可以使用。

使用 ng-model =“ x.qty”

但是,要获得Over-All total,您必须在JS文件中编写total()函数。

更新1:

HTML文件:

<body>  
    <div data-ng-app = "myapp" data-ng-controller = "CartForm">
        <table>
            <tr ng-repeat = "x in names">
                <td>{{ x.price }}</td>
                <td><input type = "number" ng-model = "x.qty" /></td>
                <td>{{ x.qty * x.price | currency: "Rs " }}</td>
            </tr>
            <tr>
                <td>Total:</td>
                <td>{{ total() }}</td>
            </tr>
        </table>
    </div>
</body>

Angular JS文件:

var myApp = angular.module('myApp', []);
myApp.controller("CartForm", function ($scope) {
    $scope.names = [
        {price:2000, qty:3},
        {price:7000, qty:6},
        {price:3000, qty:5}
        ];

    // Function for performing total of all.
    $scope.total = function() {
        var total = 0;
        for(var i = 0; i < $scope.names.length; i++){
            var name = $scope.names[i];
            total = total + (name.price * name.qty);
    }
    return total;
    };
});

答案 1 :(得分:0)

尝试类似的方式。

$scope.getTotal = function(){
    var total = 0;
    for(var i = 0; i < $scope.cart.products.length; i++){
        var product = $scope.cart.products[i];
        total += (product.price * product.quantity);
    }
    return total;
}