$ scope变量未在每个

时间:2017-02-04 13:24:56

标签: angularjs

我正在制作一个简单的项目计算,我将项目价格,数量和标题存储在一个数组中。我正在计算每个项目的总金额:

<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);

中每个项目的正确计算

请告诉我为什么在输出中它只显示最后的计算。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要为每个定义的项目 -(void)viewWillAppear:(BOOL)animated { [self.navigationController setNavigationBarHidden:NO]; NSDictionary *dictValue = [DataValue objectAtIndex:0]; self.navigationItem.title = [dictValue valueForKey:@"code"]; // NSDictionary *dictValue1 = [DataValue objectAtIndex:1]; // self.navigationItem.title = [dictValue1 valueForKey:@"code"]; // NSDictionary *dictValue2 = [DataValue objectAtIndex:2]; // self.navigationItem.title = [dictValue2 valueForKey:@"code"]; }

<强> totalprice

&#13;
&#13;
DEMO
&#13;
var app = angular.module('sampleApp', []);
app.controller("myCtrl", function($scope) {
$scope.items= [
{title: 'Paint Pots', quantity: 8, price: 3.95,totalprice:0},
{title: 'Polka Pots', quantity: 17, price: 6.95,totalprice:0},
{title: 'Pebbles', quantity: 5, price: 12.95,totalprice:0}
];
$scope.totalprice=0;
for(var i=0; i<$scope.items.length; i++){
$scope.items[i].totalprice = $scope.items[i].price * $scope.items[i].quantity;
}


});
&#13;
&#13;
&#13;