Questions tagged [rootscope]

Every AngularJS application has a single root scope. All other scopes are descendant scopes of the root scope. Scopes provide separation between the model and the view, via a mechanism for watching the model for changes. They also provide an event emission/broadcast and subscription facility.

Every part of an AngularJS application has a scope. At the ng-app level, this scope is called the $rootScope. When Angular starts to run and generate the view, it creates a binding from the root ng-app element to the $rootScope. This $rootScope is the eventual parent of all $scope objects. So we can say that the $rootScope object is the closest object we have to the global context in an Angular app.

$rootScope is deprecated. Using it will make migration to Angular 2+ more difficult.

Resources

176 questions
219
votes
8 answers

How do I use $rootScope in Angular to store variables?

How do I use $rootScope to store variables in a controller I want to later access in another controller? For example: angular.module('myApp').controller('myCtrl', function($scope) { var a = //something in the scope //put it in the root…
trysis
  • 8,086
  • 17
  • 51
  • 80
116
votes
5 answers

Why do we use $rootScope.$broadcast in AngularJS?

Tried to find some basic information for AngularJS $rootScope.$broadcast, But the AngularJS documentation doesn't help much. In easy words why do we use this? Also, inside John Papa's Hot Towel template there is a custom function in the common…
Nexus23
  • 6,195
  • 9
  • 50
  • 67
49
votes
6 answers

Angularjs - $rootScope in directive link function

I am asking this question because I am not quite clear on how to think of rootscope as a dependency passed to directives I have a directive that needs to display some information from $rootScope ... I thought I needed to pass the $rootScope to a…
GRowing
  • 4,629
  • 13
  • 52
  • 75
43
votes
3 answers

How to access/update $rootScope from outside Angular

My application initializes an object graph in $rootScope, like this ... var myApp = angular.module('myApp', []); myApp.run(function ($rootScope) { $rootScope.myObject = { value: 1 }; }); ... and then consumes data from that object graph (1-way…
Tim Coulter
  • 8,705
  • 11
  • 64
  • 95
25
votes
3 answers

Angular $rootScope.$broadcast() event caught twice in controller

Broadcating event on button click :- $scope.onButtonClick = function(){ $rootScope.$broadcast('onButtonClick'); } And catching event in another controller :- $rootScope.$on('onButtonClick',function(event){ alert("catched"); …
Pratibha
  • 257
  • 1
  • 3
  • 10
14
votes
4 answers

Retain page data on refreshing the page

I am new to angular. I am using a service which gets a list of objects and displays them on the 1st page. Then based on what object was clicked, I am setting the tab header on the next page. But while I am refreshing the page the scope of the list…
Nutan
  • 1,478
  • 3
  • 12
  • 19
13
votes
3 answers

Why using $rootScope with functions is not recommended?

While am looking into the FEQs of Angularjs I've seen below article: $rootScope exists, but it can be used for evil Scopes in Angular form a hierarchy, prototypally inheriting from a root scope at the top of the tree. Usually this can be ignored,…
10
votes
1 answer

How to access rootscope in component view

I have angular controller dashboard and set the root scope value in dashboard and access that root scope value in component inside the dashboard template using scope..but i don't know whether this is correct or not..and am not able to get that…
Trent
  • 105
  • 3
  • 12
9
votes
1 answer

how to delete $rootScope variable in angularjs?

In a controller, I had defined $rootScope.tableformate variable to fill data in a popup. After filling popup data $rootScope.tableformate is not used. How to delete this variable?
Nimesh Vaghasiya
  • 887
  • 4
  • 13
  • 28
8
votes
3 answers

How to reset $rootScope?

After user logs out, I need to clean up my $rootScope. I tried $rootScope.$destroy() but that didn't do the trick. Is there a way to loop through all the values in $rootScope and delete them or a method to simply reset it?
Adam Boostani
  • 5,999
  • 9
  • 38
  • 44
8
votes
1 answer

Detecting page unload in angularjs

I have a javascript object which I need global to my angularjs application. When the application is about to be unloaded (due to a page refresh or some other scenario), I would like to persist the object to browser storage. I believe adding the…
user1491636
  • 2,355
  • 11
  • 44
  • 71
8
votes
1 answer

Redirect state in angularjs

This is the state configuration: angular .module('grabhutApp', [...]) .config(function ($stateProvider, $urlRouterProvider) { $stateProvider // ACCOUNT .state('account', { abstract: true, …
fiberOptics
  • 6,955
  • 25
  • 70
  • 105
6
votes
1 answer

Difference between $rootScope and $rootScope.$root

Is there any difference between $rootScope and $rootScope.$root? What is the difference between $rootScope.global.flag = true and $rootScope.$root.global.flag = true Do both of them access the same variable in rootscope? If so, is there any…
Nithin Baby
  • 7,486
  • 4
  • 21
  • 25
6
votes
1 answer

$digest already in progress when calling $rootScope.$apply() in quick succession

So I have an AngularJS service listening for some events. On the handling of these events I need to call different controllers and ultimately load a new view. In one event handler I am using $location.path() then calling $rootScope.apply() to…
user1491636
  • 2,355
  • 11
  • 44
  • 71
6
votes
2 answers

How to compare against global variables in ng-switch

I am using the AngularJS $rootScope object to expose some global constants that need to be accessible to both controllers and views: var app = angular.module('myApp', []); app.run(function ($rootScope) { $rootScope.myConstant = 2; }); When I…
Tim Coulter
  • 8,705
  • 11
  • 64
  • 95
1
2 3
11 12