我正在使用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/

10-10 00:28