Firebase authentication on App initilisation

前端 未结 1 1980
失恋的感觉
失恋的感觉 2021-01-29 07:27

This is works:

console.log(\'User ID: \' + user.id + \', Provider: \' + user.provider);

but this one is not:

$scope.authenticat         


        
相关标签:
1条回答
  • 2021-01-29 08:05

    Most likely, you're running into a problem with Angular's HTML Compiler.

    Whenever you use an event like ng-click/ng-submit/etc, Angular fires $scope.$apply(), which checks for any changes to your $scope variables and applies them to the DOM.

    Since FirebaseSimpleLogin is not part of Angular's purview, it has no idea that when the callback is fired, you've updated $scope.authenticated.currentUser. This would also explain why it works when you call auth.login(), since you're probably invoking that via an ng-click event somewhere, which would fire a digest check and discover the changes.

    If this is indeed the case, you can correct this issue by alerting Angular that it needs to run $apply by using $timeout:

    crossfitApp.controller('globalIdCtrl', ["$scope",'defautProfileData','$q', '$timeout', function ($scope,defautProfileData,$q, $timeout) {
    
    /* ... */
    
    var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
      if (error) {
        /* ... */
      } else if (user) {
        $timeout(function() {
           $scope.authenticated.currentUser = user.id ;//
        });
      } else {
        // user is logged out
      }
    });
    
    0 讨论(0)
提交回复
热议问题