【问题标题】:Another issue after AngularFire 0.5.0 update - calculation returns NaNAngularFire 0.5.0 更新后的另一个问题 - 计算返回 NaN
【发布时间】:2014-01-10 17:29:16
【问题描述】:

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

【问题讨论】:

  • 您在 $scope.totalEarning 的日志中得到了什么?你能用一个简单的 jsfiddle/plunkr 重现这个吗?
  • 对于 typeof $scope.totalEarning 我得到数字。对于 $scope.totalEarning,我只得到 NaN。
  • 这是一个缩短的 plunkr 复制品:embed.plnkr.co/jb1iWOcjcm0alFchmzOH/preview

标签: angularjs firebase angularfire


【解决方案1】:

更新答案

您做错了几件事。正如我原来的答案所述,您需要遍历 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;
}

【讨论】:

  • 似乎效果更好。问题是,它不会将最新的商品价格添加到总价格中。当我添加一个新项目时,它会添加最后一个项目的价格。并且由于某种原因,它不再执行 totalBalance 了。我现在也收到错误:TypeError: Cannot read property 'price' of undefined
  • 非常感谢。这是有道理的,在 firebase 更新后更新。它现在工作正常。 :)
猜你喜欢
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 2014-09-02
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 2016-07-22
相关资源
最近更新 更多