AngularJS $watch root scope variable for changes

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)