为什么要使用angularfire $ destroy()?

Dir*_*irk 11 angularfire

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

angularfire sais的文档:

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

停止侦听此阵列使用的事件和可用内存(清空本地副本).更改不再与Firebase同步或从Firebase同步.

sync = $firebase(ref).$asArray();
...
....
sync.$destroy()
Run Code Online (Sandbox Code Playgroud)

我不能这样做:

sync = null
Run Code Online (Sandbox Code Playgroud)

要么

delete sync
Run Code Online (Sandbox Code Playgroud)

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

Cor*_*lva 4

存在的目的$destroy() 是清空数据并取消绑定事件侦听器。 如果您需要取消绑定$firebaseArray()或$firebaseObject()可以使用$destroy(),但我认为使用已解决的取消绑定功能会更好。

这是从angularfire-seed中获取的代码片段

  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();
    ...
  };
Run Code Online (Sandbox Code Playgroud)