Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $watch root scope variable for changes

Tags:

angularjs

I have the following $rootScope variable which I use to save the current logged in user privilege level, then I access this variable from other controllers. Is there a way I can watch the rootScope variable for changes in order to update controllers specific variables with any changes to the root scope variable? Below is the code I am using so far, can someone please tell me what I am doing wrong and how to fix it? Thanks

In app.js under .run:

 $rootScope.uPLevel = 0; 

.controller

   $scope.$watch($rootScope.uPLevel, function() {             $scope.userPLevel = $rootScope.uPLevel;    }, true); 
like image 342
MChan Avatar asked Mar 29 '14 23:03

MChan


People also ask

How do I use $Watch in AngularJS?

When you create a data binding from somewhere in your view to a variable on the $scope object, AngularJS creates a "watch" internally. A watch means that AngularJS watches changes in the variable on the $scope object. The framework is "watching" the variable. Watches are created using the $scope.

What is rootScope apply ()?

$scope.$apply() This function is used to execute an expression in Agular. The function expression is optional and you can directly use $apply(). This is used to run watcher for the entire scope. $rootScope.$digest()

What is the scope of scope in AngularJS?

AngularJS Scope The scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.

What is $emit $broadcast and $on in AngularJS?

The difference between $broadcast() and $emit() is that the former sends the event from the current controller to all of its child controllers. That means $broadcast() sends an even downwards from parent to child controllers. The $emit() method, on the other hand, does exactly opposite.


2 Answers

The first parameter to $watch should either be a string or a function (docs). Right now you're passing it the value of $rootScope.uPLevel on controller initialization.

$scope.$watch(function() {   return $rootScope.uPLevel; }, function() {   $scope.userPLevel = $rootScope.uPLevel; }, true); 

Two sidenotes:

  • It may be prudent to store this value in a service instead of $rootScope.
  • If uPLevel is only an integer (as your example suggests) then you don't need to pass true as the third parameter - that's only for arrays and objects. If you do want to watch a collection, then I suggest using $watchCollection instead.
like image 159
SomeKittens Avatar answered Sep 18 '22 17:09

SomeKittens


I recommend watching $rootScope variables like that:

$scope.$watch('$root.uPLevel', function() {     $scope.userPLevel = $rootScope.uPLevel; }); 

This way, When current directive/controller is destroyed. It clears the watch as well. In $rootScope.$watch case, the watch stays forever.

like image 32
nir Avatar answered Sep 16 '22 17:09

nir