【问题标题】:How to disassociate angularFire bound object?如何解除 angularFire 绑定对象的关联?
【发布时间】:2013-08-09 10:37:06
【问题描述】:

我在我的应用程序中使用 AngularJS 和 FireBase。我绑定了一个与 FireBase 同步的对象:

$scope.winnerPromise = angularFire(travelBidsFirebaseRef + "/user/" + $scope.auction.winnerUserId, $scope, 'winner', {});

现在我想解除 $scope.winner 的关联,这意味着我希望它在 FireBase DB 中保持安全,但我不希望我的范围变量 'winner' 再与它同步。我怎么做?我在 angularfire.js 中看到了 disassociate() 函数,但我不知道如何使用它。有什么想法吗?

【问题讨论】:

    标签: angularjs firebase


    【解决方案1】:

    当 promise 被解决时,disassociate 函数会传递给你。我会按如下方式使用它:

    var ref = travelBidsFirebaseRef.child("user/" + $scope.auction.winnerUserId);
    var promise = angularFire(ref, $scope, "winner", {});
    promise.then(function(disassociate) {
      // Do some work...
      disassociate(); // Don't synchronize $scope.winner anymore.
    });
    

    希望这会有所帮助!

    【讨论】:

    • 它有帮助,谢谢!将 disassociate() 函数附加到范围对象会很棒,例如$scope.winner.disassociate();因为通常并非所有代码都在 promise.then() 函数中。我实际上试图这样做: $scope.winner.disassociate = disassociate;但是,每次对象更改时,绑定的范围对象引用(例如 $scope.winner)都会重新分配一个新对象。
    • 是的,不幸的是,由于 Angular 观察对象更改的方式,我不建议将函数附加到绑定到 Firebase URL 的 $scope 对象。您可以将函数存储在单独的 $scope 变量下,例如 $scope.disassociateWinner()
    【解决方案2】:

    我正在使用 angularfire,对我来说它适用于 $destroy 方法。

    https://github.com/firebase/angularfire/blob/master/docs/reference.md#destroy-1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多