【问题标题】:Why use angularfire $destroy()?为什么使用 angularfire $destroy()?
【发布时间】:2015-01-28 15:28:19
【问题描述】:

$destroy() 存在于 Angularfire 中的原因是什么?

angularfire sais 的文档:

https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-destroy

停止监听事件并释放此数组使用的内存(清空本地副本)。更改不再与 Firebase 同步。

sync = $firebase(ref).$asArray();
...
....
sync.$destroy()

我可以不做吗:

sync = null

delete sync

或者出于某种原因我真的应该使用 $destroy() 吗?

【问题讨论】:

  • Looking at this line 如果您正在销毁范围,您可能甚至不需要调用 $destroy。如果你没有破坏范围,你应该做类似于this

标签: angularfire


【解决方案1】:

$destroy()exists to empty the data and unbind event listeners. 如果你需要解绑$firebaseArray()$firebaseObject(),你可以使用$destroy(),但我认为使用解绑函数会更好。

这是来自angularfire-seed的代码sn-p

  var unbind;
  // create a 3-way binding with the user profile object in Firebase
  var profile = $firebaseObject(fbutil.ref('users', user.uid));
  profile.$bindTo($scope, 'profile').then(function(ub) { unbind = ub; });

  // expose logout function to scope
  $scope.logout = function() {
    if( unbind ) { unbind(); }
    profile.$destroy();
    ...
  };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    相关资源
    最近更新 更多