javascript – Is it possible to use Controllers hierarchically in Angular JS?

Question:

Controllers are declared in the DOM via the ng-controller attribute. My question is: Is it possible and robust / safe to define Controllers hierarchically in the DOM in child tags?

Example:

<div ng-controller="CtrlAbrangente">
  <div ng-controller="CtrlNoDIVFilhoDeAbrangente">
    Fazer algo que usa o escopo do CtrlAbrangente e/ou do CtrlNoDIVFilhoDeAbrangente.
  </div>
</div>

I see this need in cases where functionality on one page can be reused on others (more Comprehensive).

Answer:

Yes, it is possible and commonly used.

A scenario would be that you have a contact page with a ContactCtrl controller and inside this page you have a contact form with the ContactFormCtrl controller where you would do the validations and the like.

You could also use ContactFormCtrl somewhere else, a footer contact form for example, depending on your application of course.

Hope this helps

Scroll to Top