I am currently creating a new property in $rootScope and setting its value in one module.
$rootScope.test = 123;
I am then trying to reference this value later in a service function.
.factory("testFactory", function ($location, $http, $rootScope) {
/* .... */
return {
testFunction : function(){
console.log($rootScope);
console.log($rootScope.test);
},
/* .... */
When I view the console output in Chrome, I can see that the value of test is being set properly in the $rootScope object, but I am unable to reference using the $rootScope.test syntax. $rooScope.test simply returns undefined.
Is there any reason that you can't reference property values of $rootScope in services? Or am I attempting to retrieve this value improperly?
UPDATE - I have created a Plunker that demonstrates the issue that I am running into.
I am currently creating a new property in $rootScope and setting its value in one module.
$rootScope.test = 123;
I am then trying to reference this value later in a service function.
.factory("testFactory", function ($location, $http, $rootScope) {
/* .... */
return {
testFunction : function(){
console.log($rootScope);
console.log($rootScope.test);
},
/* .... */
When I view the console output in Chrome, I can see that the value of test is being set properly in the $rootScope object, but I am unable to reference using the $rootScope.test syntax. $rooScope.test simply returns undefined.
Is there any reason that you can't reference property values of $rootScope in services? Or am I attempting to retrieve this value improperly?
UPDATE - I have created a Plunker that demonstrates the issue that I am running into. http://plnkr.co/edit/ePEiYh
Share Improve this question edited Aug 21, 2013 at 14:31 ral8 asked Aug 21, 2013 at 13:02 ral8ral8 1531 gold badge1 silver badge7 bronze badges 4-
How about trying
.factory("testFactory", ["$location", "$http", $rootScope", function($location, $http, $rootScope) { ... }]);
– tymeJV Commented Aug 21, 2013 at 13:05 -
1
Make sure that you are doing
$rootScope.test = 123;
before you dotestFactory.testFunction();
– AlwaysALearner Commented Aug 21, 2013 at 13:08 - How did you solve this? I'm running through the exact same problem. In the console I can see the attribute in the first console.log but in the second when trying to print the attribute itself it es as undefined – victor hugo Commented Jan 16, 2015 at 2:20
- I ended up using $dispatch on the $rootScope to notify the factory function when the value was set, which is shown in an example here - youtube./watch?v=1OALSkJGsRw . This might not be the most elegant solution, but worked for my scenario. – ral8 Commented Jan 16, 2015 at 15:11
1 Answer
Reset to default 9This should work just fine:
var myApp = angular.module("myApp", []);
myApp.run(['$rootScope', function($rootScope){
$rootScope.test = 123;
}]);
myApp.controller('AppController', ['$rootScope', function ($rootScope) {
console.log($rootScope.test);
}]);
Plunker: http://plnkr.co/edit/7NTGOK
I guess that you are reading the value before you are writing it.
UPDATE
The debugging experience is really weird here. However, in my updated plunker you can see through the timestamps that the writing happens after reading:
http://plnkr.co/edit/pn5Wxk