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.
weil Sie neu zu berechnen und '$ scope.totalprice' Wert Neuzuweisung, deshalb letzter Wert auf' $ scope.totalprice' –
ok zugewiesen wird, Bitte geben Sie eine Lösung im Code. – user3450590