Ad

Why The Interceptor Factory Is Failing When Is Request In The Config File?

I have the next configuration (index.js) to call to my interceptor factory.

angular.module('pasApp')
 .factory('InterceptorFactory',['$q','$location',require('./factory-interceptor.js')])
 .config(['$stateProvider','$urlRouterProvider', '$httpProvider','InterceptorFactory',require('./config-app.js')])
 .run(['$ionicPlatform','$rootScope','$window','StorageFactory','$state','$timeout','$http',require('./run-app.js')]);

My folder and files order:

>config
  >config-app.js
  >factory-interceptor.js
  >index.js
  >run-app.js

When I call my "InterceptorFactory" function from './factory-inteceptor.js', my console presents the next error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module pasApp due to:
Error: [$injector:unpr] Unknown provider: InterceptorFactory
http://errors.angularjs.org/1.4.3/$injector/unpr?p0=InterceptorFactory
at http://localhost:8100/js/app.bundle.js:9874:12
at http://localhost:8100/js/app.bundle.js:14068:19
at getService (http://localhost:8100/js/app.bundle.js:14215:39)
at Object.invoke (http://localhost:8100/js/app.bundle.js:14247:13)
at runInvokeQueue (http://localhost:8100/js/app.bundle.js:14162:35)
at http://localhost:8100/js/app.bundle.js:14171:11
at forEach (http://localhost:8100/js/app.bundle.js:10142:20)
at loadModules (http://localhost:8100/js/app.bundle.js:14152:5)
at createInjector (http://localhost:8100/js/app.bundle.js:14078:11)
at doBootstrap (http://localhost:8100/js/app.bundle.js:11436:20)
http://errors.angularjs.org/1.4.3/$injector/modulerr?p0=pasApp&p1=Error%3A%…otstrap%20(http%3A%2F%2Flocalhost%3A8100%2Fjs%2Fapp.bundle.js%3A11436%3A20)
Ad

Answer

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

This way I can't inject a factory in configuration blocks. This factory would be inject before the configuration block is added to application.

Angular documentation

Ad
source: stackoverflow.com
Ad