Load all dependencies dynamically

3

In the index.php file I include all controllers:

<script src="app/app.js"></script>
<script src="app/ctrl_A.js"></script>
<script src="app/ctrl_B.js"></script>
<script src="app/ctrl_C.js"></script>
//etc...

This is not good, since I am loading javascript files that are not missing, for example, on the homepage.

In the .config of the app I'm referring to the controller as follows:

app.config(['$urlRouterProvider', '$stateProvider', '$locationProvider', 
function($urlRouterProvider, $stateProvider, $locationProvider) {
    $urlRouterProvider.otherwise("/login");
      $stateProvider
        .state('login', {
            url: "/login",
            templateUrl: "view/login.html",
            controller: 'ctrl_A'
        })
//...

How do I make the dependencies (controller, factory, directive, ect ...) dynamically loaded without declaring in index.php?

    
asked by anonymous 13.11.2014 / 18:10

1 answer

1

I think the best way is to use angularAMD . It basically links AngularJS with an asynchronous Javascript module load definition called AMD, which does exactly what you expect.

Your index would only look like a script like this:

<script data-main="app/app.js" src="app/libs/require.js"></script>

Turning your code into AMD modules brings benefits to maintainability of your project is not a difficult task.

    
09.01.2015 / 01:49