我有一個錯誤導致我瘋了,基本上我所要做的就是設置一個$rootScope.authData
屬性,該屬性持有用戶的身份驗證信息我可以在不同的控制器中使用它。
但是,當我嘗試這個,它只是給了我一個錯誤,說$rootScope.authData
沒有定義。我已經檢查過,當它從mainCtrl
登錄到控制檯時確實定義了它,但在從tagsCtrl
將它登錄到控制檯時確實是undefined
。
這是奇怪的考慮的事實,我可以在我的其他控制器的一個使用$rootScope.authData
..同時我,如果我在mainCtrl
和控制檯日誌添加$rootScope.test = 'testing'
,在tagsCtrl
它的工作原理。
我看不出我在這裏完成的任何錯誤,並且我已經到了死衚衕。有任何想法嗎?
主控制器,設置$rootScope.authData
:
flickrApp.controller('tagsCtrl', ['$scope', '$rootScope', '$firebase', function($scope, $rootScope, $firebase) {
console.log($rootScope.authData); //Is not defined here
}]);
編輯:
flickrApp.controller('mainCtrl', ['$scope', '$rootScope', '$firebase', 'Auth', function($scope, $rootScope, $firebase, Auth) {
Auth.$onAuth(function(authData) {
$rootScope.authData = authData;
console.log($rootScope.authData); //Is defined here
});
}]);
無法訪問$rootScope.authData
控制器從Bricktop一些反饋後,我試圖創建一個服務對此,結果如下:
flickrApp.service('shared', ['$scope', 'Auth', function($scope, Auth) {
//Auth is a wrapper that points to my firebase reference
Auth.$onAuth(function(authData) {
return $scope.authData = authData;
});
}]);
我不知道這是否會是一個有效的但現在看來,這不是因爲我得到這個錯誤:
Error: [$injector:unpr] http://errors.angularjs.org/1.3.10/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20shared
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:6:417
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:38:307
at Object.d [as get] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:36:308)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:38:381
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:36:308)
at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:64)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:213)
at Object.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:490)
at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:96)
我注入這樣的:
flickrApp.controller('tagsCtrl', ['$scope', '$firebase', 'shared', function($scope, $firebase, shared) {
console.log($scope.authData.uid); //This would come from the 'shared' service now
}]);
什麼是錯在這裏?
我試過了,但是我失敗了。看看更新的問題。 – Chrillewoodz 2015-02-06 20:02:37
好的,我會在幾分鐘內編輯我的答案。 – Bricktop 2015-02-06 20:15:48
似乎我甚至無法將服務注入控制器出於某種原因......走向精神。 – Chrillewoodz 2015-02-06 20:36:16