问题描述:

I have following code in angularjs .run()

so when the app is initial, I set 2 vars under rootScope.

Because these vars I decide to use in every routes (pages) and controllers.

async

app.run(function($rootScope, $timeout, firebaseAuth) {

$rootScope.$on('firebaseLoggedIn', function(event, authUser) {

$timeout(function() {

$rootScope._isLoggedIn = true;

$rootScope._authUser = authUser;

},0);

});

But the problem here, when I run it in my controllers, it always show undefined.

unless i put it in function block. So my guess is the controllers is run

before the .run() vars have value;

app.controller('foo', function($scope){

console.log($scope._authUser); //Always undefined

//unless I do following

//$scope.clickme = function() {console.log($scope._authUser)};

});

Any suggestion I am able to use the vars(async) from .run()?

If I call the following code in every controllers, it seems like I keep repeating myself.

$rootScope.$on('firebaseLoggedIn', function(event, authUser) {});

UPDATE

Here is where firebaseLoggedIn event from. I think I have to mention again. The callback is async.

app.service('firebaseAuth', ['$rootScope',

function($rootScope) {

//Define firebase DB url

this.firebaseRef = new Firebase("https://test.firebaseio.com");

this.onLoginStateChanges = new FirebaseAuthClient(this.firebaseRef, function(error, user) {

if (user) {

$rootScope.$emit("firebaseLoggedIn", user); //authenticated user

}

else if (error) {

$rootScope.$emit("firebaseLoginError", error); //authentication failed

}

else {

$rootScope.$emit("firebaseLogout"); //user not login

}

});

}]);

网友答案:

The run method will be ran when the injector is done loading all modules for your app. So your assumption about your controller being instantiated first is true.

What you're effectively doing with the $timeout(function() { ... },0) is forcing that code to run in the next digest loop. But I don't think this is what you're trying to accomplish anyway.

I'm not sure where 'firebaseLoggedIn' is being broadcasted from; but you may find this entire implementation would be more straight forward as an Angular Service.

app.service('FireBaseService', function($rootScope) {
   // store these inside the service to be retrieved via methods below.
   var isLoggedIn = false;
   var authUser = {};

   // let this be the only place you handle the LoggedIn event, setting user context. 
   $rootScope.$on('firebaseLoggedIn', function(event, authUser) {
      isLoggedIn = true;
      authUser = authUser;   
   });

   // return some functions to expose the variables above
   return {
      getIsLoggedIn: function() {
         return isLoggedIn;
      },
      getAuthUser: function() {
         return authUser;
      }
   }
 });

Then in your controller......

app.controller('foo', function($scope, $log, FireBaseService){
   if (FireBaseService.getIsLoggedIn()){
      $log.info(FireBaseService.getAuthUser().Name);
   }
});

This enables you to have access to these variables anywhere you've injected the FireBaseService service.

相关阅读:
Top