应用/提供商循环依赖(App/provider circular dependency)

编程入门 行业动态 更新时间:2024-10-27 14:30:37
应用/提供商循环依赖(App/provider circular dependency)

我想在启动我的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.

更多推荐

本文发布于:2023-08-04 16:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1418951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:提供商   App   provider   circular   dependency

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!