3

当我的模块没有加载时,我该如何正确加载它?

假设我有几十个控制器,我想将每个控制器分成自己的文件。对于这个例子,假设我有一个控制器存在于一个文件中:controller.js

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

我有一个从以下位置加载的模块:base.js

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

我从我的 html 加载这些文件中的每一个:

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

当我按上述顺序加载文件时,我无权访问 pubmodule 所以我看到:

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

我密切关注这个问题,但这个问题没有考虑加载顺序,这是我感兴趣的主题。如何将我的控制器分成不同的文件并考虑我的模块加载顺序?只需更改我的 html 加载顺序即可解决此错误,但我担心在考虑延迟时我无法控制此错误。对 controller.js 的请求可能会先返回。

4

2 回答 2

0

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

于 2014-07-31T19:40:27.257 回答
0

就加载顺序而言,延迟无关紧要。浏览器仍将加载在 html 中声明的文件。因此,只要首先引用您的 base.js 文件就可以了。浏览器将按照它们出现的顺序执行代码。因此,如果控制器在 base 之后声明但首先下降,则在 base 完成之前不会对其进行评估。

于 2014-07-31T21:53:03.893 回答