我对angularFire 0.5.0越来越失望,因为没有什么能正常工作了。在我能够帮助您修复单个项目的删除后,我遇到了另一个问题。
每个单独的项目包括日期,描述和价格。在我更新之前,我能够计算所有价格的总和并将其返回到页面上。现在它只是说NaN或Null。我一直在试图找出原因,但计算中的两个值(earning.price,$ scope.totalEarning)都是数字。我不明白。新的角火会做什么吗?我试图解决它一段时间,但不能。如果有人能搞清楚的话,我会的。可能我只是没有看到它,这是一个非常愚蠢的问题。
在plunkr上查看:http://embed.plnkr.co/jb1iWOcjcm0alFchmzOH/preview
以下是代码:
$scope.addEarning = function() {
$scope.earnings.$add({date:$scope.FormEarningDate, description:$scope.FormEarningDescription, price:$scope.FormEarningPrice});
$scope.FormEarningDate = '';
$scope.FormEarningDescription = '';
$scope.FormEarningPrice = '';
$scope.updateEarning();
}
$scope.updateEarning = function() {
$scope.totalEarning = 0;
angular.forEach($scope.earnings, function (earning) {
price = parseFloat(earning.price)
$scope.totalEarning += price;
$log.log(typeof $scope.totalEarning);
})
$scope.totalBalance = $scope.totalEarning - $scope.totalCost;
}
和html:
<form for="Profit" class="form-inline" style="margin-bottom: 20px" ng-submit="addEarning()">
<div class="form-group">
<div class="col-sm-3">
<input type="date" name="idate" ng-model="FormEarningDate" class="form-control" id="idate" placeholder="Date">
</div>
<div class="col-sm-5">
<input type="text" name="idesc" ng-model="FormEarningDescription" required class="form-control" id="idesc" placeholder="Description">
</div>
<div class="col-sm-2">
<input type="text" name="iprice" ng-model="FormEarningPrice" required class="form-control" id="iprice" placeholder="Amount">
</div>
</div>
<tr ng-repeat="earning in earnings | orderByPriority | orderBy : 'date'">
<td>{{earning.date}}</td>
<td>{{earning.description}}</td>
<td>{{earning.price}} €</td>
<td><button class="btn btn-danger" ng-click="earnings.$remove(earning.$id)">Löschen</button></td>
</tr>
<tr>
<td colspan="2"></td>
<td><strong>Total:</strong> {{totalEarning}} €</td>
<td></td>
</tr>
答案 0 :(得分:2)
更新了答案
你做错了几件事。正如我在原始答案中所述,您需要迭代Firebase中的键,而不是firebase对象本身。此外,您需要确保在Firebase更新后(通过$on('change', ...)
事件处理程序)并在AngularJS生命周期内(使用$timeout
在下面完成)发生所有更新。
var app = angular.module('balance', ['firebase']);
app.controller('BalanceCtrl', function($scope, $log, $http, $timeout, $firebase) {
$scope.earnings = $firebase(new Firebase('https://dgdemo.firebaseio.com/Earnings'));
$scope.totalBalance = 'Nothing yet.';
$scope.totalEarning = 0;
$scope.totalCost = 0;
$scope.updateEarning = function() {
$scope.totalEarning = 0;
angular.forEach($scope.earnings.$getIndex(), function (id) {
var earning = $scope.earnings[id];
$scope.totalEarning += parseFloat(earning.price);
});
$scope.totalBalance = $scope.totalEarning - $scope.totalCost;
};
// Ensure any call to updateCallback will execute within the AngularJS lifecycle.
var updateCallback = angular.bind(null, $timeout, $scope.updateEarning);
$scope.addEarning = function() {
var earning = $scope.earnings.$add({
date: $scope.FormEarningDate,
description: $scope.FormEarningDescription,
price: $scope.FormEarningPrice
});
$scope.FormEarningDate = '';
$scope.FormEarningDescription = '';
$scope.FormEarningPrice = '';
};
$scope.earnings.$on('change', updateCallback);
});
原始答案
这是因为angular.forEach
正在迭代$scope.earnings
对象(Firebase对象)的所有属性。你想要做的是迭代各个项目。
$scope.updateEarning = function() {
$scope.totalEarning = 0;
angular.forEach($scope.earnings.$getIndex(), function (id) {
var earning = $scope.earnings[id];
$scope.totalEarning += parseFloat(earning.price);
});
$scope.totalBalance = $scope.totalEarning - $scope.totalCost;
}