A
A
Alexey Yarkov2016-04-17 10:21:14
Angular
Alexey Yarkov, 2016-04-17 10:21:14

How to inject a factory into a factory?

I'm trying to write a $http interceptor. And in it, when receiving a response, you must call the method of one factory. It is logical to assume that you need to inject this factory. As a result, this code:

/*global angular*/
(function () {
  'use strict';

  angular
    .module('App')
    .factory('httpErrorResponceInterceptor', ['$rootScope', 
                								  //'authFactory', 
                								  '$log', 
                								  httpErrorResponceInterceptor]);

  httpErrorResponceInterceptor.$inject = ['$rootScope', 
                							//'authFactory', 
                							'$log'];

  /*@ngInject*/
  function httpErrorResponceInterceptor($rootScope, 
                    					  //authFactory, 
                    					  $log) {
    var self = {};

        self.response = function(response) {

            $log.debug('call authFactory.errorResponse');
            //authFactory.errorResponse(response);
            $log.debug('authFactory.errorResponse called');
            return response;

        }

    return self;
  }

})();

authFactory is the same factory. If you uncomment the lines mentioning it, then the application crashes on startup. CHADNT?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2016-04-17
@healqq

You either write dependencies in an array:

.factory('httpErrorResponceInterceptor', ['$rootScope',
     //'authFactory', 
     '$log', 
     httpErrorResponceInterceptor]);

Or via $inject:
httpErrorResponceInterceptor.$inject = ['$rootScope', 
    'authFactory', 
    '$log'];

These are equivalent records, and it is not necessary to write both.
In general, it would be nice to attach a stack trace of the error. Does the factory run on its own?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question