Angularjs AngularFire 0.5.0更新后的另一个问题-计算返回NaN

Angularjs AngularFire 0.5.0更新后的另一个问题-计算返回NaN,angularjs,firebase,angularfire,Angularjs,Firebase,Angularfire,我对angularFire 0.5.0越来越失望,因为再也不能正常工作了。在您的帮助下,我修复了单个项目的删除后,我遇到了另一个问题 每个项目包括日期、说明和价格。在我更新之前,我能够计算所有价格的总和并将其返回到页面上。现在它只显示NaN或Null。我已经在试图找出原因,但计算中的两个值(earning.price、$scope.totalEarning)都是数字。我不明白。新的有角火有什么作用吗?我已经试着修了一段时间了,但就是修不好。如果有人能弄明白的话,我会很高兴的。也许我只是没有看到,

我对angularFire 0.5.0越来越失望,因为再也不能正常工作了。在您的帮助下,我修复了单个项目的删除后,我遇到了另一个问题

每个项目包括日期、说明和价格。在我更新之前,我能够计算所有价格的总和并将其返回到页面上。现在它只显示NaN或Null。我已经在试图找出原因,但计算中的两个值(earning.price、$scope.totalEarning)都是数字。我不明白。新的有角火有什么作用吗?我已经试着修了一段时间了,但就是修不好。如果有人能弄明白的话,我会很高兴的。也许我只是没有看到,这是一个相当愚蠢的问题

请在plunkr上查看:

代码如下:

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

{{earning.date}
{{earning.description}
{{赚取价格}}€
洛申
总计:{{totalLearning}}

更新答案

有几件事你做得不对。正如我最初的回答中所述,您需要迭代Firebase中的键,而不是Firebase对象本身。此外,您需要确保所有更新都发生在Firebase更新之后(通过('change',…)事件处理程序)和AngularJS生命周期内(以下使用
$timeout
完成)

原始答案

这是因为
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;
}

$scope.totalEarning的日志中有什么内容?您能在一个简单的JSFIDLE/plunkr中重现这一点吗?对于$scope.totalLearning类型,我得到一个数字。只要$scope.totalEarning,我就得到了NaN。这是一个缩短的plunkr复制:它似乎工作得更好。问题是,它没有将最新的商品价格添加到总价格中。当我添加一个新项目时,它会添加最后一个项目的价格。出于某种原因,它不再做总平衡了。我现在也得到了错误:TypeError:无法读取未定义的属性“价格”,非常感谢。这是有道理的,在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;
}