我想在启动我的AngularJs应用程序时使用提供程序设置一些配置设置,但我希望将应用程序和提供程序放在2个单独的文件中。
这就是我现在的方式:
var myApp = angular.module('myApp', []); myApp.config(function(myServiceProvider) { ....etc.提供者定义如下:
angular.module('myApp').provider('myService', function () { ...etc.如果我首先加载应用程序,那么提供程序还没有,如果我首先加载提供程序,那么应用程序还没有。 解决这个问题的最佳方法是什么?
I want to use a provider to set some configuration settings at starting up my AngularJs app, but I would like to have the app and the provider in 2 seperate files.
This is how I have it now:
var myApp = angular.module('myApp', []); myApp.config(function(myServiceProvider) { ....etc.The provider is defined like this:
angular.module('myApp').provider('myService', function () { ...etc.If I load the app first the provider is not there yet, and if I load the provider first the app is not there yet. What would be the best way to solve this?
最满意答案
您的模块定义包括模块依赖性 - 而不是服务或提供者。
它应该是:
var myApp = angular.module('myApp', []);初始化提供程序后,始终会执行配置块。 没有循环依赖问题。
文件1:定义模块
angular.module('myApp', []);文件2:定义提供者
angular.module('myApp').provider('myService',function() {...});文件3:定义配置块
angular.module('myApp').config(function(myServiceProvider) { ... });HTML:
<script src="/angular.js"></script> <script src="/file1.js"></script> <script src="/file2.js"></script> <script src="/file3.js"></script>文件顺序很重要。
Your module definition includes module dependencies - not services or providers.
It should be:
var myApp = angular.module('myApp', []);The config block is always executed after your providers are initialized. There is no circular dependency issue.
File 1: Define module
angular.module('myApp', []);File 2: Define providers
angular.module('myApp').provider('myService',function() {...});File 3: Define config block
angular.module('myApp').config(function(myServiceProvider) { ... });HTML:
<script src="/angular.js"></script> <script src="/file1.js"></script> <script src="/file2.js"></script> <script src="/file3.js"></script>Order of the files is important.
更多推荐
发布评论