I'm encountering a peculiar issue with the $rootScope.$on
method within a service or Factory.
Here's an example of the service:
app.service('testService', function($rootScope){
this.var1 = 5;
this.change = function(n){
this.var1 = n;
}
$rootScope.$on('service', function(event, n){
//this.var1 = n;
console.log(this.var1);
});
});
The problem lies in the fact that it should display 5
in the console. However, I am unable to access the service's variables within the $rootScope.$on
method...
Below is the corresponding controller:
app.controller( 'HelloCtrl', function($scope, testService, testFactory, $rootScope)
{
$scope.fromService = testService;
$rootScope.$broadcast("service", 12);
});
Here is the link to the JSFiddle showcasing the issue: jsFiddle
What could be the root cause of this problem?
Any suggestions on how to resolve this issue?