如何在控制器和服务中使用 angularFireAuth 值? [英] How to use angularFireAuth values in controllers and services?

查看:21
本文介绍了如何在控制器和服务中使用 angularFireAuth 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我将 angularFireAuth 放在服务中.

Let say I put angularFireAuth inside a service.

app.factory('authService',
function($rootScope, $timeout, angularFireAuth, FIREBASE_URL) {
    return function() {
        angularFireAuth.initialize(new Firebase(FIREBASE_URL), {
            scope: $rootScope,
            name: 'user',
            path: "/"
        });
        $rootScope.$on('angularFireAuth:login', function _set(evt, user) {
            $timeout(function() {
                $rootScope.auth = {
                    authenticated: true
                   };
            });
         });
        $rootScope.$on('angularFireAuth:logout', function(){
            $timeout(function() {
                $rootScope.auth = {
                    authenticated: false
                   };
            });    
        });
      }
    });

然后我在 .run() 中初始化它

Then I initial it in .run()

.run(['$rootScope', 'authService', function($rootScope, authService){
  authService();
});

我的问题是如何在其他服务和控制器中使用 $scope.auth.authenticated.

My question is how can I use $scope.auth.authenticated in other services and controllers.

console.log($scope.auth.authenticated) 在控制器中.它总是返回false.好像不是在听/看登录

I console.log($scope.auth.authenticated) in controller. It always return false. Seem like it is not listening / watching the login

我可以直接在控制器中使用 $scope.user 并监听它吗?

Can I use $scope.user directly in controller and listening on it?

更新

我创建了一个 plunker.

推荐答案

问题是 $scope.user(以及扩展名 $scope.auth)在登录发生之前不会被创建,直到单击一次登录按钮.但是console.log 事件会在控制器创建后立即发生 (onDOMReady).

The problem is that $scope.user (and by extension $scope.auth) are not created until the login takes place, which isn't until one clicks the login button. But the console.log event takes place as soon as the controller is created (onDOMReady).

您可能根本不需要 $scope.auth.Firereader 使用它是因为它需要将一些额外的数据放入用户对象中,特别是在 ng-switch 语句中使用的布尔值.在您的情况下,您可能只使用 $scope.user,它由 angularFireAuth 设置,而不必理会 $scope.auth 对象.

You probably don't need the $scope.auth at all. Firereader is using that because it needs some additional data put into the user object and specifically a boolean which is used in an ng-switch statement. In your case, you can probably just use $scope.user, which is set by angularFireAuth and not bother with the $scope.auth object.

所以,总而言之,如果您移动 console.log 以等待登录完成,它将按预期工作:http://plnkr.co/edit/Bd23DGFtEpqO2g4jo06v?p=preview

So, to summarize, if you move your console.log to wait until the login is completed, it will work as expected: http://plnkr.co/edit/Bd23DGFtEpqO2g4jo06v?p=preview

var app = angular.module('plunker', ['firebase']);

app.constant('FIREBASE_URL', 'https://newname.firebaseio.com/');
app.run(['$rootScope', 'authService', function($rootScope, authService){
    authService();
    $rootScope.$watch('auth.authenticated', function() {
        isAuthenticated = $rootScope.auth.authenticated;
    });
}]);

app.factory('authService', [
    '$rootScope',
    '$timeout',
    'angularFireAuth',
    'FIREBASE_URL',

    function($rootScope, $timeout, angularFireAuth, FIREBASE_URL) {
        return function() {
            angularFireAuth.initialize(new Firebase(FIREBASE_URL), {
                scope: $rootScope,
                name: 'user'
            });

            $rootScope.$on('angularFireAuth:login', _log);

            function _log(evt, user) {
                // this is where $scope.user and $scope.auth will be set
                  console.log($scope.user);
             }
        }
    }
]);



app.controller('MainCtrl', function($scope, authService, angularFireAuth) {
  $scope.name = 'World';

  $scope.login = function(){
    angularFireAuth.login('facebook');
  }

  $scope.logout = function(){
    angularFireAuth.logout();
  }

  // angularFireAuth hasn't returned yet at this point
  // console.log($scope.auth);
});

这篇关于如何在控制器和服务中使用 angularFireAuth 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆