2017-02-04 1 views
1

aktualisiert Ich mache eine einfache Item-Berechnung wo ich Artikelpreis, Menge und Titel in einem Array gespeichert habe. Ich bin der Berechnung der Gesamtbetrag für jedes der Elemente:

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

in Skript:

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 = 

}); 

aber das Problem ist, dass es den berechneten Wert von {{Totalprice}} zeigt nur die letzte Artikel, während die Konsole zeigt die richtige Berechnung für jeden Artikel in $log.log($scope.items[i].price*$scope.items[i].quantity);

Bitte sagen Sie mir, warum in der Ausgabe zeigt es nur letzte Berechnung. Danke im Voraus.

+0

weil Sie neu zu berechnen und '$ scope.totalprice' Wert Neuzuweisung, deshalb letzter Wert auf' $ scope.totalprice' –

+0

ok zugewiesen wird, Bitte geben Sie eine Lösung im Code. – user3450590

Antwort

1

Sie müssen für jedes definierte Element totalprice haben.

DEMO

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

 

 
});
<!DOCTYPE html> 
 
<html ng-app="sampleApp" xmlns="http://www.w3.org/1999/xhtml"> 
 
<head> 
 
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.10/angular.min.js"></script> 
 
</head> 
 
<body ng-controller="myCtrl"> 
 
    <table> 
 

 
<tr ng-repeat="itms in items"><td>{{itms.title}} <input type="text" ng-model="itms.quantity"/>{{itms.price}} - {{itms.totalprice}}</td></tr> 
 

 

 
</table> 
 
</body> 
 
</html>

+0

danke. Aber nur neugierig: Kann ich den Wert von $ scope.totalprice nicht herausnehmen, OHNE ihn dem Array hinzuzufügen? besonders wenn die Konsole '$ log.log ($ scope.items [i] .price * $ scope.items [i] .quantity);' die richtigen Werte wirft? – user3450590

+0

Ich bekomme es nicht, Sie brauchen nicht $ scope.totalvalue – Sajeetharan

+0

Ich sage nur, dass wenn ich die richtigen Werte in $ log.log ($ scope.items [i] .price * $ scope.items [i] bekomme .quantity) ', warum es nicht im {{total price}} widerspiegelt. Ich denke, wir könnten etwas wie $ watch tun, um Werte zu aktualisieren, so etwas Ähnliches, ohne 'Gesamtpreis' innerhalb des Arrays hinzuzufügen. – user3450590

Verwandte Themen