【问题标题】:Why isn't angularFire binding to scope after previous unbind/disassociate?为什么在先前的取消绑定/解除关联后 angularFire 不绑定到范围?
【发布时间】:2013-11-06 20:07:05
【问题描述】:

我正在尝试的是:登录时,将配置文件信息添加到范围,然后在注销时使用取消关联回调(取消绑定),这样一个用户的垃圾不会与下一个用户混合。

它在初始登录时有效,但在注销后,登录失败。有什么建议么?总的来说,我已经注意到这一点。

    $scope.$on('angularFireAuth:login', function(){
        //attach current profile info to scope
        angularFire(fbUrl + 'profiles/user-' + $scope.auth.id , $scope, "profile",{}).
        then(function(unbind){
            console.log($scope.profile);
            $scope.$on('angularFireAuth:logout', function(){
                    //detach current profile info from scope
                    unbind();
            });
        });
    });

【问题讨论】:

  • 另外..有没有更好的方法来使用回调。
  • 登录失败怎么办?没有用户能够进行身份验证?
  • **** 它没有失败。我的测试数据有重复值。抱歉@Hiattp .. 感谢您抽出宝贵时间。
  • Np,我建议将该事件侦听器从回调中取出,只是为了清理它。

标签: angularjs firebase angularfire


【解决方案1】:

正如您所指出的,您不能有重复的值:)。由于您还询问是否有更好的方法来使用回调,所以这里有一些额外的想法。我建议从 Promise 回调中删除注销事件侦听器,原因如下:

  1. 您最终可能希望对该事件执行与登录事件无关的其他操作,因此您希望保持关注点分离。
  2. 您可能希望在应用程序的其他位置取消绑定配置文件信息,因此无论如何将其附加到 $scope 是有意义的。

你可能想做更多这样的事情:

$scope.$on('angularFireAuth:login', function(){
  //attach current profile info to scope
  angularFire(fbUrl + 'profiles/user-' + $scope.auth.id , $scope, "profile",{}).
  then(function(unbind){
    $scope.unbindProfile = unbind;      
  });
});

$scope.$on('angularFireAuth:logout', function(){
  //detach current profile info from scope
  if($scope.unbindProfile) $scope.unbindProfile();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2015-07-21
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多