Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting a service into another service in angularJS

Is it possible to inject one service into another service in angularJS?

like image 565
Subtubes Avatar asked Jan 08 '14 19:01

Subtubes


People also ask

Can we inject service into service in Angular?

You can inject an Angular service in a component, service, directive etc by specifying the service and its type in a component's constructor. Note that injecting a service through a class constructor is, in general, tree-shakable.

What is dependency injection in AngularJS?

Dependency Injection (DI) is a software design pattern that deals with how components get hold of their dependencies. The AngularJS injector subsystem is in charge of creating components, resolving their dependencies, and providing them to other components as requested.


1 Answers

Yes. follow the regular injection rule in angularjs.

app.service('service1', function(){});  //Inject service1 into service2 app.service('service2',function(service1){}); 

Thanks to @simon. It is better to use Array injection to avoid minifying problem.

  app.service('service2',['service1', function(service1) {}]); 
like image 157
Alborz Avatar answered Sep 24 '22 13:09

Alborz