gpt4 book ai didi

angularjs - 依赖项是否共享?

转载 作者:行者123 更新时间:2023-12-03 08:06:15 24 4
gpt4 key购买 nike

假设我有 3 个模块:

angular.module('A', [])

angular.module('B', ['A'])

angular.module('C', ['B', 'A'])

在依赖注入(inject)方面,将 BC共享相同的模块实例 A ,还是将单独的实例注入(inject)到每个实例中?

最佳答案

总结你的问题,答案只是一个例子。

事实上就是这样。 Angular 应用程序通过注入(inject)器解决依赖关系。每个应用程序只创建一个注入(inject)器。从技术上讲,您只能拥有一个 ng-app ,但是您可以使用手动引导来拥有多个应用程序,在这种情况下,将为每个应用程序创建一个注入(inject)器,并且这两个应用程序不会共享任何依赖项。

在一般情况下,只有 rootElement 引导为 Angular 应用程序。它解析启动已引导模块的模块(从依赖链的底部开始)。在该模块下注册的任何服务/ Controller /过滤器等或该依赖链下的任何依赖模块都将在注入(inject)器缓存中捆绑在一起(注入(inject)时延迟实例化)一次。例如说你有一个服务myService在模块 A 下注册.不管你列出多少个地方A由于依赖项最终只有一个构造函数实例可用,并且作为单例的服务每个人都获得相同的单例实例。

Official Doc :

Modules can list other modules as their dependencies. Depending on a module implies that the required module needs to be loaded before the requiring module is loaded. In other words the configuration blocks of the required modules execute before the configuration blocks of the requiring module. The same is true for the run blocks. Each module can only be loaded once, even if multiple other modules require it.

关于angularjs - 依赖项是否共享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30653248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com