0

Consider the html is

<div ng-controller="parentCtrl">
//some content
   <div ng-controller="childCtrl">
      <div ng-show = "mycommonscopevar">show my message </div>
      //some content
</div>
</div>

in my parent controller, i am setting "mycommonscopevar" as false initially and then in some function i am setting mycommonscopevar as true. When i click on a tab , my child controller will get called , now i need to set that mycommonscopevar as false again. I mean switching from one controller to another the scope variable has to be reset. Is this possible? Could anyone help me on this. Thanks in advance.

2
  • docs.angularjs.org/api/ng/service/$rootScope add $rootScope to both controllers, declare your variable as $rootScope.mycommonscopevar = someValue; $rootScope is common scope for ALL controllers Commented Dec 3, 2015 at 13:58
  • Thanks for your reply..but i am not sure i am missing out something or not. but it is not working for me. Commented Dec 3, 2015 at 14:24

2 Answers 2

1

Child can access and change the parent controller scope by

// Consider Parent Controller
function Parent($scope) {
    $scope.Test = 'value';
};

function Child($scope,$rootScope) {
    $scope.$parent.Test = ' $scope.Test value of parent can be changed here';
    $scope.ofChild = 'anyvalue'; // Cannot be accessed by the parent
    $rootScope.ofChild = 'can be accessed byy all controllers';
};

But the parent cannot access directly , better you can use services or $rootScope

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

3 Comments

could you kindly give any example for both cases
Edited the answer please check it out
Thanks for your reply
0

You can just change the value of $scope.mycommonscopevar to true in your child (if they share the same scope) and it will be reflected in the parent scope as well.

3 Comments

sorry i am not clear. What you mean by if there share the same scope. i need to reset the value when i switch from one controller to another. Thanks for your reply
Maybe you should read something about scopes in AngularJS first: github.com/angular/angular.js/wiki/Understanding-Scopes
If possible can you explain with some simple example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.