5

我在运行中定义了一个 $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)

});

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

};

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

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

4

1 回答 1

0

你注入了 $firebase 模块吗?

function myCtrl($rootScope, $scope, $firebase) {
   ...    
};
于 2014-12-29T17:41:54.860 回答