我有一个错误导致我疯了,基本上我所要做的就是设置一个$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