Question

When my module does not load how can I load it correctly?

Suppose I have tens of controllers and I would like to separate each controller into its own file. For this example suppose I have one controller that lives in a file: controller.js

angular.module('pubmodule').controller( 'CreatePostController', ['$stateParams', '$scope','$http' ,function($stateParams, $scope, $http) {
        }]);

and I have a module which loads from: base.js

angular.module( 'pubmodule', ['ngSanitize', 'ionic'] )
.run( function ( $rootScope, $state, $stateParams, PostTimelineService) {} );

I load each of these files from my html:

<script type="text/javascript" src="controller.js"></script>
<script type="text/javascript" src="base.js"></script>

When I load the files in the above order, I do not have access to pubmodule so I see:

Error: [ng:areq] Argument 'CreatePostController' is not a function, got undefined

I closely followed this question, but this question does not consider load order and that is the topic I am interested in. How can I separate my controllers into different files AND consider the order my module loads? Simply changing the order my html loads solves this error, but I am concerned that I do not have control over this when considering latency. The request for controller.js may come back first.

Was it helpful?

Solution

Latency doesn't matter in terms of load order. The browser will still load the files as they are declared in the html. So as long as your base.js file is referenced first you'll be ok. The browser will execute the code in order they appear. So if controller is declared after base but comes down first, then it won't be evaluated till base is done.

OTHER TIPS

My sucpicion is the module.run() occuring after the controller.js in base.js.

On a side note if base.js is really the base it should be on the top of controller.js.

Nonetheless try adding your .run in the first js. But also make sure that all its dependencies are available prior to that...e.g. your PostTineLineService.

Checkout AngularJS documentation on the order of run with respect to its dependencies.

enter link description here

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top