0

I am trying to change the value of a $rootScope variable and let that variable be accessible to various controllers with the updated value. However, for some reason the changed value in $rootScope seems to go undetected by any controller accessing it via $rootScope.

In this example, why doesn't "child" reflect the changed value?

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <script data-require="[email protected]" src="http://code.angularjs.org/1.2.15/angular.js" data-semver="1.2.15"></script>
  <script type="text/javascript">
    angular.module('myApp', [])
      .run(function($rootScope) {
        $rootScope.current = 'a';
      })
      .controller('ParentCtrl', ['$scope', '$rootScope',
        function($scope, $rootScope) {
          $scope.child = $rootScope.current;
        }
      ])
  </script>
</head>

<body ng-app="myApp">
  <div>
    <p>Value of "current": {{ current }}</p>
    <button ng-click="$root.current = 'b'">Change value to 'b'</button>
    <div ng-controller="ParentCtrl">
      Value of "current" (within ParentCtrl): {{ current }}
      <br>Value of "child" (via ParentCtrl injection of rootscope): {{ child }}
    </div>
  </div>
</body>

</html>

Here is my plunker. Thanks in advance!

3
  • Because your ng-click did not update the child variable. Your child variable got initialized but never got updated. Commented Mar 24, 2015 at 20:07
  • @TechCrunch I'm not interested in changing the child variable specifically, though...I'm interested in the child value auto-updating to reflect the "current" variable it was pointing to. Is there another mechanism that is designed to observe changes to $rootScope? Commented Mar 24, 2015 at 20:09
  • 1
    yes, you should use $watch or $watchCollection Commented Mar 24, 2015 at 20:26

2 Answers 2

2

You must watch for rootScope variable as below.

 $scope.$watch(function() {
            return $rootScope.current;
          }, function() {
              $scope.child = $rootScope.current;
            }, 
          true);

Here is the working plunker. You can read more about $watch here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks; this is precisely what I needed!
0

This way should work, check Angularjs scope doc for better understanding

https://docs.angularjs.org/guide/scope

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
  document.write('<base href="' + document.location + '" />');
</script>
<script data-require="[email protected]" src="http://code.angularjs.org/1.2.15/angular.js" data-semver="1.2.15"></script>
<script type="text/javascript">
angular.module('myApp', [])
  .run(function($rootScope) {
    $rootScope.current = 'a';
  })
  .controller('ParentCtrl', ['$scope', '$rootScope',
    function($scope, $rootScope) {
      $scope.root = $rootScope;
    }
  ])
 </script>
 </head>

<body ng-app="myApp">
  <div>
  <p>Value of "current": {{ current }}</p>
  <button ng-click="$root.current = 'b'">Change value to 'b'</button>
  <div ng-controller="ParentCtrl">
  Value of "current" (within ParentCtrl): {{ current }}
  <br>Value of "child" (via ParentCtrl injection of rootscope): {{ root.current }}
  </div>
 </div>
  </body>
 </html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.