在创建后向 Angular 模块添加依赖项 [英] Add dependency to Angular module after it's been created

查看:23
本文介绍了在创建后向 Angular 模块添加依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们知道是否可以在创建后添加模块依赖项?类似的东西:

Do you guys know if it's possible to add a module dependency after it's been created? Something like that:

// init module in file A
angular.module("myApp", []);

// retrieve module in file B and add new dependencies:
mod = angular.module("myApp");
// now I want to add an 'ngResource' dependency to mod
// ???

我知道这似乎是一个奇怪的问题.我正在研究基于组件的应用程序组织,并且还想看看子组件是否可以在不定义自己的模块的情况下向其父组件模块添加依赖项.类似的东西:

I understand this may seem a weird question. I am working on a component based app organization and want too see if a subcomponent can add dependencies to it parent component module without defining its own module. Something like that:

app/
 components/
   |__componentA/ 
        |__app.js // initializes component A module
        |__subcomponentA/ 
              |__app.js // want to add subcomponentA dependencies to componentA module from here

我的替代方法是直接在 componentA 模块上声明所有 subcomponentA 依赖项,但从组织的角度来看,我更愿意将这些依赖项保留在 subcomponentA 目录中,因此如果我以后决定从应用程序中删除 subcomponentA,我不需要记住从 componentA 模块中删除它的依赖项.我希望与 subcomponentA 相关的所有内容都分组在 subcomponentA 目录中.不过,我的构建脚本将确保在处理 subcomponentA 之前处理 componentA 代码.

My alternative is simply declare all subcomponentA dependencies directly on the componentA module, but from organization point of view, I'd prefer to keep these dependencies inside the subcomponentA directory, so if I later decide to remove the subcomponentA from the app, I don't need to remember to remove its dependencies from the componentA module. I want everything that concerns subcomponentA to be grouped inside the subcomponentA directory. My build script will ensure that componentA code is processed before the subcomponetA is, though.

感谢所有为此付出努力的人.

Thank you to everyone who takes a stab at this.

推荐答案

我为此使用了以下方法并且对我来说效果很好.

I use the following method for this and working fine for me.

var app = angular.module("myApp", []);

angular.module("myApp").requires.push('ngResource');

这篇关于在创建后向 Angular 模块添加依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆