【发布时间】:2015-02-07 04:51:33
【问题描述】:
我有一个让我发疯的错误,基本上我要做的就是设置一个$rootScope.authData 属性,该属性包含用户的身份验证信息,以便我可以在不同的控制器中使用它。
但是,当我尝试这个时,它只是给我一个错误,说 $rootScope.authData 没有定义。我已经检查过,它确实是从mainCtrl 将其记录到控制台时定义的,但从tagsCtrl 将其记录到控制台时它是undefined。
考虑到我可以在我的其他控制器之一中使用$rootScope.authData,这很奇怪。如果我在mainCtrl 中添加$rootScope.test = 'testing' 并在tagsCtrl 中添加控制台日志,它可以工作.
我看不出我在这里做错了什么,我已经走到了死胡同。有什么想法吗?
设置$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
}]);
这里有什么问题?
【问题讨论】:
标签: angularjs angularfire rootscope