javascript - 计算 ng-repeat Angular JS 表达总数

标签 javascript angularjs angularjs-ng-repeat

我正在使用 Angular 构建一个报价系统。我显示了不同的项目,但我正在努力计算总成本。请看下面的代码:

<tr ng-repeat="part in curOrder.orderParts">
    <td>{{part.partDesc}}</td>
    <td>{{part.partQty}}</td>
    <td>{{part.partPrice * part.partQty | number:2}}</td>
</tr>
<tr>
    <td>&nbsp;</td>
    <th>Total</th>
    <td>
        {{(part.partPrice * part.partQty)*(curOrder.orderParts.length) | number:2}}&nbsp;{{curOrder.currency}}
        //I know this line doesn't work as it isn't in the ng-repeat.
        //How would I store this expression as a variable so I could use it here?
    </td>
</tr>

如何将 {{part.partPrice * part.partQty}} 存储为变量(可能是“itemTotal”),然后将所有它们添加到“orderTotal”之类的内容中并将其显示在页面上?

我应该提到的是,用户可以在此过程中通过输入字段更改每个零件的价格。这导致当用户更改价格时变量不更新的问题。其代码如下:

$scope.orderParts = $scope.curOrder.orderParts;
for($part in $scope.orderParts){
$partTotal = parseInt($scope.orderParts[$part].partQuotePrice * $scope.orderParts[$part].partQty);
$scope.curOrder.partTotal += $partTotal;
console.log(
    "\n partQuotePrice: "+$scope.orderParts[$part].partQuotePrice + //Prints 0 as nothing in user input yet
    "\n partQty: "+ $scope.orderParts[$part].partQty + //Prints 100
    "\n Multipled: "+($scope.orderParts[$part].partQuotePrice * $scope.orderParts[$part].partQty)+ //Prints 0 (0*100)
    "\n partQuotePriceTotal:"+$scope.curOrder.partTotal //Prints NaN
);

}

任何帮助将不胜感激。

谢谢。

最佳答案

您似乎正在使用 $scope.curOrder.partTotal += $partTotal; - 您是否曾将 $scope.curOrder.partTotal 设置为 0?

x = undefined
x += 2 // x == NaN

如果您的 $scope.curOrder.partTotal 变量尚未初始化,则将数字添加到 undefined 将为您提供 NaN 结果.

关于javascript - 计算 ng-repeat Angular JS 表达总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190919/

相关文章:

javascript - 如何将字符串导出到带有换行符的文本文件? AngularJS

javascript - 使用默认值创建新的 Angular $resource?

javascript - Angular Directive(指令)双向范围不更新模板

JavaScript - 声明具有功能的函数

Javascript 如何在 CPU Uint8array 上重现 GLSL 浮点矩阵运算

javascript - 扩展 AngularJS Controller 的推荐方法是什么?

angularjs - 嵌套在惰性一次性 ng-repeat 绑定(bind)内部的绑定(bind)是否只绑定(bind)一次?

javascript - 使用 thymeleaf 获取 JS 文件中的图像

javascript - 当AS的ExternalInterface准备好时告诉JS

angularjs - 内联 ngRepeat 对象数组