Dependency injection in service

2

Can I do dependency injection on services? for example, I tried this:

service 1

var crypto = angular.module('crypto',['ngRoute']);

service 2

var teste = angular.module('userlog',['crypto']);
teste.service('userlogService','cryptoService', function (cryptoService) {

but it is giving error

    
asked by anonymous 20.10.2015 / 21:20

1 answer

1

In the service, it works differently:

var teste = angular.module('userlog',['crypto']);

teste.service('seuService', ['userlogService','cryptoService', function (userlogService, cryptoService) {
//aqui o serviço
}]);

If you will not use it elsewhere, you can also do so only:

  teste.service('seuService', function (userlogService, cryptoService) {
    //aqui o serviço
    });

If you intend to use multiple methods within the same service, I recommend that you use a factory instead of service:

 var teste = angular.module('userlog',['crypto']);

 teste.factory('seuService', function () {

      function seuService() {

          var userlogService = function(scope) {
          //service 1
          };

          var cryptoService = function(scope) {
          //service 2
          }; 
      }
      return new seuService(); 
    });

Using the controller would look something like this:

teste.controller('seuController',['seuService',function(seuService) {
    seuService().userlogService($scope);
    seuService().cryptoService($scope);
}]);
    
20.10.2015 / 21:24