0

I am using angularjs, load and register controller, service and factory lazy loaded.

My configuration file is similar to Dave's Answer which is used for registration and work fine.

Now i want to create a separate file containing factory definition which is lazy loaded and use as interceptor. For the same purpose i created app.interceptor function in config file which has definition as $httpProvider.interceptors.push('MyService'); but whenever ajax request is send it will not call. Any idea why is not working Eg: myapp.config.js

var app = angular.module('app', [])
app.config(
  function($controllerProvider, $provide, $compileProvider) {
    // Since the "shorthand" methods for component
    // definitions are no longer valid, we can just
    // override them to use the providers for post-
    // bootstrap loading.
    console.log("Config method executed.");
    // Let's keep the older references.
    app._controller = app.controller;
    app._service = app.service;
    app._factory = app.factory;
    app._value = app.value;
    app._directive = app.directive;
    app.controller = function(name, constructor) {
      console.log("controller...");
      console.log(name);
      console.dir(constructor);
      $controllerProvider.register(name, constructor);
      return (this);
    };
    // Provider-based service.
    app.service = function(name, constructor) {
      $provide.service(name, constructor);
      return (this);
    };
    // Provider-based factory.
    app.factory = function(name, factory) {
      $provide.factory(name, factory);
      return (this);
    };
    // Provider-based value.
    app.value = function(name, value) {
      $provide.value(name, value);
      return (this);
    };
    // Provider-based directive.
    app.directive = function(name, factory) {
      $compileProvider.directive(name, factory);
      return (this);
    };
    app.interceptor = function(name) {
     console.log("interceptor ----" + name)
     $httpProvider.interceptors.push(name);
     };
  });

MyFactory.js

var app = angular.module('app');
app.interceptor('myfactory');
app.factory('myfactory',  ['$q', '$location',function ($q, $location) {
   return {
         // optional method
         request: function(config) {
           // do something on success
            console.log("request config");
                        console.log(config);
           return config;
         },

         // optional method
        requestError: function(rejection) {
           // do something on error
            console.log("requestError");
                        console.log(rejection);
           return $q.reject(rejection);
         },



         // optional method
         response: function(response) {
           // do something on success
            console.log("response response");
                        console.log(response);
           return response;
         },

         // optional method
        responseError: function(rejection) {
           // do something on error
            console.log("responseError");
           console.log(rejection);

           return $q.reject(rejection);
         }

      }

}]);
4
  • The .config doesn't inject $httpProvider. Commented Apr 16, 2019 at 19:37
  • What error message are you getting? Is it TypeError: app.interceptor is not a function? Commented Apr 16, 2019 at 19:45
  • @georgeawg there is no error message Commented Apr 17, 2019 at 5:59
  • @georgeawg as i declared app.interceptor in config it get called when i app.interceptor('myfactory') from MyFactory.js but when i send any ajax it is not intercept the request Commented Apr 17, 2019 at 6:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.