Angularjs $scope变量未在每个

Angularjs $scope变量未在每个,angularjs,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}}</t

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

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

{{itms.title}{{itms.price}}-{{totalprice}}
手稿:

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 = 

});
app.controller(“myctrl”,函数($scope,$log){
$scope.items=[
{标题:“油漆罐”,数量:8个,价格:3.95},
{标题:'Polka Pots',数量:17个,价格:6.95},
{标题:“鹅卵石”,数量:5个,价格:12.95}
]
//$log.log($scope.items[0].title);
//总价=数量*价格
$scope.totalprice=0;

对于(var i=0;i,您需要为定义的每个项目提供
总价

演示

var-app=angular.module('sampleApp',[]);
app.controller(“myCtrl”,函数($scope){
$scope.items=[
{标题:“油漆罐”,数量:8个,价格:3.95,总价:0},
{标题:'Polka Pots',数量:17,价格:6.95,总价:0},
{标题:“鹅卵石”,数量:5,价格:12.95,总价:0}
];
$scope.totalprice=0;

对于(var i=0;i因为您正在重新计算并重新分配
$scope.totalprice
值,这就是为什么最后一个值被分配给
$scope.totalprice
好的,请用代码给出解决方案。@user3450590您可以通过单击答案旁边的勾号来标记答案吗。您在标记答案题上的历史记录很差。但好奇的是:我可以不做吗如果不将$scope.totalprice添加到数组中,就不能取出它的值吗?特别是当控制台
$log.log($scope.items[i].price*$scope.items[i].quantity);
抛出正确的值时?我不明白,你不需要$scope.totalvalue我只是说当我在
$log.log($scope.items[i]中获得正确的值时.price*$scope.items[i].quantity)
,那么为什么它没有反映在{{totalprice}}中。我想我们可以做一些事情,比如$watch来保持值的更新,所以类似的事情,而不在数组中添加
totalprice