$ rootscope值在Controller中未定义

Jas*_*son 5 angularjs rootscope

我在运行中定义了一个$ rootscope对象,如下所示。

//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)

});
Run Code Online (Sandbox Code Playgroud)

$ rootScope.user包含userId,userName,userEmail等。

稍后,在控制器中,我想在$ rootScope.user中使用一些数据,但是它是未定义的!

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);

};
Run Code Online (Sandbox Code Playgroud)

令我发疯的是,当myCtrl在chrome javascript控制台中显示$ rootScope时,我可以展开对象并查看用户对象!

但是第二个console.log只是未定义的。请帮忙!

Nic*_*aro 0

你注入了 $firebase 模块吗?

function myCtrl($rootScope, $scope, $firebase) {
   ...    
};
Run Code Online (Sandbox Code Playgroud)