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.
$broadcastfrom parent to child controller. This approach is not expensive because broadcasting happens once. Anyways its not good practice regards your example.$rootScope.watch('variable', ...)?