计算 ng-repeat Angular JS 表达式的总数
Calculating total of ng-repeat Angular JS expression
我正在使用 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> </td>
<th>Total</th>
<td>
{{(part.partPrice * part.partQty)*(curOrder.orderParts.length) | number:2}} {{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
结果。
我正在使用 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> </td>
<th>Total</th>
<td>
{{(part.partPrice * part.partQty)*(curOrder.orderParts.length) | number:2}} {{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
结果。