Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use $rootScope inside a factory?

Tags:

angularjs

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.

like image 890
TheNickyYo Avatar asked Jan 16 '14 12:01

TheNickyYo


People also ask

How to use rootScope in AngularJS?

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.

What is rootScope broadcast in AngularJS?

$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.


1 Answers

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");
like image 195
Nahn Avatar answered Oct 02 '22 17:10

Nahn