Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a case where I need to nest controllers. I do not want the child scope to inherit from the parent scope because I have scope variables with the same names in both controllers. Is there a way to do this?

I am trying to prevent the childController's variable from being updated when the parentController's variable is changed.

<div ng-controller="parentController">
     {{myVariable}}
     <div ng-controller="childController">
          {{myVariable}}
     </div>
</div>

I am aware that I can change the name of the child controller's scope variable to avoid the inheritance problem completely, but I am looking for an alternative. Is there any known way to do this?

Kind of like isolating the scope of a directive, but isolated or private controller scope.

Edit: I have discovered that the child controller will inherit the parent controller scope var value when there is no default value for that var set in the child controller.

Compare: http://jsfiddle.net/4qcqdb6z/ to http://jsfiddle.net/4qcqdb6z/2/

share|improve this question
    
What about creating a transclude directive with an empty scope? and your childController attached –  apairet Aug 14 at 17:45
    
That's interesting! I'll take a look into that. Do you mind providing an example of what you have in mind? –  Chris B Aug 14 at 17:45
    
What exactly is the problem? The child div will display the myVariable from the child controller, and the parent div will display the myVariable from the parent controller. Isn't that what you want? –  JB Nizet Aug 14 at 17:54
2  
No, the parent can't override anything in the child scope. –  runTarm Aug 14 at 18:06
1  
The way you did it is fine. You can also consider using the controller as syntax (docs.angularjs.org/api/ng/directive/ngController), but I dislike it personally. –  JB Nizet Aug 14 at 19:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.