0
votes

I am trying to write a $httpProvider interceptor factory but running into issues, I think it's with the factory syntax. The error is 'Failed to instantiate module...' when adding the factory as a dependency in the config function.

So my question is how do I write an interceptor factory that can be exported to the config function?

// Factory
export class GlinterceptorFactory {
  constructor ($http, $q, $rootScope, $log) {
    'ngInject';
    this.$http = $http;
    this.$q = $q;
    this.$rootScope = $rootScope;
    this.$log = $log;
  }

  request(config) {
    return this.$log.log('request config factory: ', config);
  }

}

// Config function.
export function config ($logProvider, $httpProvider, glinterceptor) {
  'ngInject';
   $logProvider.debugEnabled(true);
   console.log(glinterceptor);
   //$httpProvider.interceptors.push(glinterceptor);

}

  // Module js.
import { config } from './index.config';
import { GlinterceptorFactory } from '../app/components/factories/glinterceptor.factory';

angular.module('ui', ['ngAnimate', 'ngCookies', 'ngTouch', 'ngSanitize', 'ngMessages', 'ngAria', 'ngResource', 'ui.router', 'ui.bootstrap', 'ngMap', 'chart.js', 'angular-carousel'])
  .config(config)
  .config(routerConfig)
  .run(runBlock)
  ... more
  .factory('glinterceptor', GlinterceptorFactory)
  ... more
1

1 Answers

0
votes

A class needs to be instantiated, before it can be used. A factory tries to invoke a function, and return the result. A service instantiates the function or class it receives. Use angular service instead of a factory.

.service('glinterceptor', GlinterceptorFactory)

You can find the recipes for creating angular service and factories under the providers doc.