Insert controller into the application from the requested template

Asked

Viewed 151 times

0

I am creating an angular application where, on the home page I last a controller to do some validations before proceeding with the application login. My question is, do I have to "instantiate" the controllers of each template only when I order that template? I ask this because I do not want to have 1 file controllers with all controllers because it is illogical in the MVC development pattern and not to mention that it delays the application startup. Putting in a draft ("kicking") I think it would be something like:

Template : registration page

<script src="js/controllers/CadastroCtrl.js"></script>
<ion-view view-title="Cadastro" ng-controller="CadastroCtrl">
    <ion-content>
        <h1>Cadastre alguma coisa</h1>
    </ion-content>
</ion-view>

Template : listing page

<script src="js/controllers/ListagemCtrl.js"></script>
<ion-view view-title="Listagem" ng-controller="ListagemCtrl">
    <ion-content>
        <h1>Liste alguma coisa que foi cadastrada no outro template</h1>
    </ion-content>
</ion-view>

Is that right? Does anyone have a better solution to handle the dependencies of each template? I gave the example of the controller but if I also have as dependency some service (Factory) or something of the kind would also fall into this question...

1 answer

1


What you need is to use a framework that manages your dependencies. So you can separate each controller, service, Directive, into separate files, and manage them to be loaded only when needed.

Take a look at Requirejs

For an example of how code works, see github of a friend.

  • 1

    right partner, I had already arrived at this solution of Requirejs and I’m already using it for some time, only I forgot to solve the question rsrs, but thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.