Thursday, 5 September 2013

When it's correct to use nested controllers in AngularJS, and when not?

When it's correct to use nested controllers in AngularJS, and when not?

I'm a beginner in AngularJs, so maybe I'm missing something important, but
I think that if we want to share information between controllers, we can
use a service instead, isn't it?.
Controllers should not know about the view (html), but if you think it,
when we use nested controllers like this:
<div ng-controller="ControllerA">
...
<div ng-controller="ControllerB">
...
</div>
</div>
and if the ControllerB use the ControllerA scope, then we are just
coupling ControllerB with ControllerA and the view (html), because the
html here determine the hierarchy of the scopes in the controllers, If we
want to change all the html with a new design for example, and the html
structure change like this:
<div ng-controller="ControllerA">
...
</div>
<div ng-controller="ControllerB">
...
</div>
Then this changes in the view also affect our ControllerB logic too
(because it use the ControllerA scope), so to me, nested controllers in
AngularJS looks like an antipattern, but maybe I'm missing something, so..
When it's correct to use nested controllers in AngularJs, and when not?
Could you give me some use cases?
Thanks

No comments:

Post a Comment