基本的に私がしようとしている$rootScope.authDataのは、ユーザーの認証情報を保持するプロパティを設定して、別のコントローラーで使用できるようにすることだけです。
ただし、これを試してみると、$rootScope.authData定義されていないというエラーが表示されます。確認したところ、 からコンソールにログを記録するときに実際に定義されていますが、 からコンソールmainCtrlにundefinedログを記録するときtagsCtrlです。
これは、他のコントローラーの 1 つで使用できるという事実を考えると奇妙です。また、コンソール ログ$rootScope.authDataを追加する$rootScope.test = 'testing'と、動作することがわかります。mainCtrltagsCtrl
私がここでやったことは何も悪いことではなく、私は行き止まりに達しました。何か案は?
を設定するメインコントローラ$rootScope.authData:
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:
flickrApp.controller('tagsCtrl', ['$scope', '$rootScope', '$firebase', function($scope, $rootScope, $firebase) {
console.log($rootScope.authData); //Is not defined here
}]);
編集: 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
}]);
ここで何が問題なのですか?