0

I have got two controllers. I define a $rootScope variable in parent controller and change it according to clicked dropdown.

I want the change to be reflected in the child controller.

Parent Controller

$rootScope.variable = {'attr1':true, 'attr2':false, 'attr3':false}//initializing
vm.clickedDropDown = function(index) {
    $rootScope.variable = {'attr1':false, 'attr2':false, 'attr3':false }
    switch (index) {
        case 1: 
            $rootScope.variable={'attr1':true, 'attr2':false, 'attr3':false}
            break;
        case 2: 
            $rootScope.variable={'attr1':false, 'attr2':true, 'attr3':false}
            break;
        case 3: 
            $rootScope.variable={'attr1':false, 'attr2':false, 'attr3':true}
            break;
    }
}

Child Controller

$rootScope.$watch($rootScope.variable,function(){
  console.log($rootScope.variable);
  console.log("changed");
},true)

The variable is changing and i am able to see the change in variable as output.

6
  • I would use $broadcast from parent to child controller. This approach is not expensive because broadcasting happens once. Anyways its not good practice regards your example. Commented Dec 9, 2015 at 18:28
  • How about $rootScope.watch('variable', ...)? Commented Dec 9, 2015 at 18:29
  • throws error as variable is undefined Commented Dec 9, 2015 at 18:30
  • Can i have the code with $broadcast. Commented Dec 9, 2015 at 18:31
  • well when you watch the $rootScope you dont need to specify it in the watch attribute so $rootScope.$watch('variable', function(newVal, oldVal,scope){//logix},true}); if this is a function then you need to return the function in the watcher.... as a rule of thumb if you dont have to watch though you should use $broadcast Commented Dec 9, 2015 at 18:32

2 Answers 2

3

Your watch is wrong.

Either:

$rootScope.$watch('variable' ,function(){
  console.log($rootScope.variable);
  console.log("changed");
},true);

or

$rootScope.$watch(
    function() { 
        return $rootScope.variable; 
    },
    function(){
        console.log($rootScope.variable);
        console.log("changed");
    },true);
Sign up to request clarification or add additional context in comments.

1 Comment

Option 2 worked out. Thanks. will mark it correct answer in few minutes
0

You can simply use scope because the child controller will inherit the parent controller and have access to any scope variables defined in scope and rootScope

HTML:

<div ng-controller="MyCtrl">
  <input type="text" ng-model="name">
  <div ng-controller="ChildCtrl">
    <pre>{{ debug }}</pre>
  </div>
</div>

JS:

function MyCtrl($scope, $rootScope) {
    $rootScope.name = 'Superhero';
}

function ChildCtrl($scope, $rootScope) {

    $rootScope.debug = '';

    $scope.$watch('name', function(newVal, oldVal) {
        $scope.debug += " value changed...";
    });
}

jsfiddle: http://jsfiddle.net/heavyhorse/aqbhco7m/

1 Comment

Take care of primitive type and inherence of scopes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.