我做了一个简单的项目计算,其中我有项目价格,数量和标题存储在一个数组中。我计算的总金额为每个项目:
<div ng-controller="myctrl">
<table>
<tr ng-repeat="itms in items"><td>{{itms.title}} <input type="text" ng-model="itms.quantity"/>{{itms.price}} - {{totalprice}}</td></tr>
</table>
脚本
:
app.controller("myctrl", function($scope, $log) {
$scope.items= [
{title: 'Paint Pots', quantity: 8, price: 3.95},
{title: 'Polka Pots', quantity: 17, price: 6.95},
{title: 'Pebbles', quantity: 5, price: 12.95}
]
//$log.log($scope.items[0].title);
//totalprice=quantity*price
$scope.totalprice=0;
for(var i=0; i<$scope.items.length; i++){
$log.log($scope.items[i].price*$scope.items[i].quantity);
//console.log($scope.items[i].price * $scope.items[i].quantity);
$scope.totalprice = $scope.items[i].price * $scope.items[i].quantity;
}
///$scope.totalprice =
});
但问题是,它显示了只有最后的{{totalprice}}计算值项目,而控制台显示每个项目的正确计算$log.log($scope.items[i].price*$scope.items[i].quantity);
请告诉我为什么在输出它只显示最后的计算。提前致谢。
因为你重新计算和重新分配'$ scope.totalprice'价值,这就是为什么最后的值被分配到'$ scope.totalprice' –
OK,请给代码解决方案。 – user3450590