【问题标题】:AngularJS with AngularFire 0.5.0 - $remove item doesn't work带有 AngularFire 0.5.0 的 AngularJS - $remove 项目不起作用
【发布时间】:2014-01-07 21:47:28
【问题描述】:

我只是想在我的第一个小 Angular 项目上多做些工作,我将 AngularFire 从 0.3.0 更新到 0.5.0,但没有什么真正起作用了。我能够恢复数据库访问权限并将项目添加到我的列表中。但是删除功能不再起作用。在我更新 AngularFire 之前,我使用 splice(index,1) 删除了一个项目。我还尝试使用 0.5.0 中的新 $remove 但它只是从列表中删除所有项目,即使我添加了一个键。

这是我的清单重复:

<tr ng-repeat="(key, earning) in earnings | orderByPriority">
    <td>{{earning.date}}</td>
    <td>{{earning.description}}</td>
    <td>{{earning.price}} €</td>
    <td>
      <button class="btn btn-danger" ng-click="removeEarning(key)">
        Löschen
      </button>
    </td>

如您所见,它只是创建了一个带有数据的 tr 并且每个项目都有一个删除按钮。当我单击特定项目上的删除按钮时,应仅将其从列表中删除,而不是全部删除。

我现在的 JS 代码:

function BalanceCtrl($scope, $log, $http, $firebase) {
    var dbEarnings = new Firebase('https://*******.firebaseio.com/Earnings');
    $scope.earnings = $firebase(dbEarnings);

    $scope.addEarning = function() {
        $scope.earnings.$add({date:$scope.FormEarningDate,  description:$scope.FormEarningDescription, price:$scope.FormEarningPrice});    
        $scope.FormEarningDate = '';
        $scope.FormEarningDescription = '';
        $scope.FormEarningPrice = '';
        $scope.updateEarning();}

        $scope.removeEarning = function (key) {
            $scope.earnings.$remove(key);   
        }

仅从列表中删除特定项目是行不通的。 0.3.0 一切正常。有人知道我能做什么吗?

【问题讨论】:

标签: javascript angularjs firebase angularfire


【解决方案1】:

orderByPriority 过滤器将集合转换为数组,导致 key 成为数字索引(0、1、2、...)——它不再是 Firebase 名称。

来自http://angularfire.com/documentation.html

AngularFire 提供 orderByPriority 过滤器来转换 $firebase 返回的对象到一个数组中。数组中的对象 按优先级排序(在 Firebase 中定义)。此外,每个 数组中的对象将定义一个 $id 属性,它将 对应于该对象的键名。

所以使用removeEarning(earning.$id)

http://plnkr.co/edit/pJf2drPwhFAVCuaBSPHq?p=preview

【讨论】:

  • 感谢您的帮助 :) 它正在工作。现在我可以继续使用我的应用了。
  • @trevor:当您对子数据执行此操作时,不会生成 $id。请参阅plnkr.co/edit/pElPkk 这是有意的吗?创建子引用确实有效。
  • @RichardVan'tHoenderdaal 是的,这也是我的经验。我使用ng-init 来存储.$child(…) 引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
相关资源
最近更新 更多