5

I have a $rootscope object defined in the run like so..

//Start the AngularJS App
var angularApp = angular.module('angularApp')

//Run at the start of the Angular Application
.run(function ($rootScope, $location, $firebase, $firebaseSimpleLogin) {

    //Create the User Object. 
    $rootScope.user = $firebase($rootScope.fb.child('/persons/person1'));

   //this works fine!
    console.log($rootScope.user.userId)

});

$rootScope.user holds userId, userName, userEmail, etc...

Later, in a controller, I want to use some data in $rootScope.user but its undefined!

function myCtrl($rootScope, $scope) {

    //need to use $rootsope.user.userId in this command...
    $scope.folders = $firebase($rootScope.fb.child('/persons/' + $rootScope.user.userId);

    //this will display the $rootScope in the console no problem!
    console.log($rootScope);

   //this just displays 'undefined'
   console.log($rootScope.user);

};

What is driving me insane is that when myCtrl displays the $rootScope in chrome javascript console, I can expand the object and SEE the user object!!!

But the second console.log is just undefined. please help!

Jason
  • 53
  • 1
  • 5
  • I'm running into this same issue and am stuck. As far as the inspector showing it, it does some kind of weird lazy evaluation. Try `if ($rootScope) console.log("it's there");` and you will see it actually is undefined at that point in time... at least for me. I'm not sure why though. Were you able to figure it out? If so, please update with the solution. – Brennan Cheung Nov 07 '14 at 23:48
  • Are you getting any other errors? It's not clear where $firebase is defined, for one thing. For another, why doesn't the first line of myCtrl fail when it tries to access $rootScope.user.userId? Obviously $rootScope.user is not undefined at that point...Maybe a more complete code example would help. – Dan Dec 29 '14 at 22:09

1 Answers1

0

Did you inject the $firebase module ?

function myCtrl($rootScope, $scope, $firebase) {
   ...    
};
Nicola Ferraro
  • 4,051
  • 5
  • 28
  • 60