I have a function inside a factory that needs to set a variable to true
on the $rootScope
. How would I apply this?
Thanks in advance.
All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.
$broadcast in AngularJS? $rootScope. $broadcast is used to broadcast a “global” event which can be caught by any listener of that particular scope. The descendant scopes can catch and handle this event by using $scope.
Inject $rootscope
dependency in the factory's function constructor and use it.
module.factory( 'factoryName', function($rootScope){
$rootScope.value = "value";
});
EDIT:
If I understood corectly, this is how you use it from the service's return statement:
module.factory('ModifyRootScopeService', function($rootScope){
return {
setRootScopeValue: function(value){
$rootScope.value = value;
}
}
});
Then, whenever you use this service (after injecting it), call
ModifyRootScopeService.setRootScopeValue("true");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With