angularjs - $rootscope 值在 Controller 中未定义

标签 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)

});

$rootScope.user 持有 userId、userName、userEmail 等...

后来,在 Controller 中,我想使用 $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 只是未定义。请帮忙!

最佳答案

你注入(inject)了 $firebase 模块吗?

function myCtrl($rootScope, $scope, $firebase) {
   ...    
};

关于angularjs - $rootscope 值在 Controller 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25152366/

相关文章:

angularjs - Angular 输入限制为 1 个字符

javascript - moment.js 使用月作为日而不是日

javascript - 无法让 ng-repeat 工作

javascript - AngularJS $rootScope 变量存在,但无法访问

angularjs - $rootScope 更改不会反射(reflect)在模板上

javascript - 为什么 Angular 根范围变量仅在两次单击事件按钮后才更新?

javascript - 使用 Javascript 和 Angularjs 循环

angularjs - 如何使用 display none 样式隐藏 md-tab

angularjs - 在angularjs中检测页面卸载

html - 如何在组件 View 中访问根作用域