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

查看:130
本文介绍了如何使用控制器和服务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 其他服务和控制器。

的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=$p$pview

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天全站免登陆