Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dependency to Angular module after it's been created

Tags:

angularjs

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 // ??? 

EDIT: 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 

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.

like image 239
demisx Avatar asked Nov 18 '14 07:11

demisx


People also ask

CAN modules have dependencies?

Dependency on libraries. If a module or mediation module needs to use resources from a library or if a library needs to use resources from another library, you have to open the module or library with the dependency editor and add a dependency on the required library.

What is the use of providedIn in Angular?

Providing a servicelink The service itself is a class that the CLI generated and that's decorated with @Injectable() . By default, this decorator has a providedIn property, which creates a provider for the service. In this case, providedIn: 'root' specifies that Angular should provide the service in the root injector.

What is the use of @injectable in Angular?

Marking a class with @Injectable ensures that the compiler will generate the necessary metadata to create the class's dependencies when the class is injected. The following example shows how a service class is properly marked so that a supporting service can be injected upon creation.

What is DEPS in Angular?

The deps property is an array of provider tokens. The Logger and UserService classes serve as tokens for their own class providers. The injector resolves these tokens and injects the corresponding services into the matching heroServiceFactory factory function parameters.


1 Answers

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

var app = angular.module("myApp", []);  angular.module("myApp").requires.push('ngResource'); 
like image 190
shah Avatar answered Oct 18 '22 17:10

shah