簡體   English   中英

$ rootscope值在Controller中未定義

[英]$rootscope value undefined in Controller

我在運行中定義了一個$ 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只是未定義。 請幫忙!

你注入$ firebase模塊了嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM