首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Angularjs加载工厂和注册https拦截器无法正常工作

Angularjs加载工厂和注册https拦截器无法正常工作
EN

Stack Overflow用户
提问于 2019-04-17 22:28:40
回答 1查看 0关注 0票数 0

我正在使用angularjs,加载和注册控制器,服务和工厂延迟加载。

现在我想创建一个单独的文件,其中包含延迟加载的工厂定义并用作拦截器。出于同样的目的,我在配置文件中创建了app.interceptor函数,该文件具有定义, $httpProvider.interceptors.push('MyService');但无论何时发送ajax请求,它都不会调用。知道为什么不工作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);
         }

      }

}]);
EN

回答 1

Stack Overflow用户

发布于 2019-04-18 07:49:11

只需使用app.config

var app = angular.module('app');
̶a̶p̶p̶.̶i̶n̶t̶e̶r̶c̶e̶p̶t̶o̶r̶(̶'̶m̶y̶f̶a̶c̶t̶o̶r̶y̶'̶)̶;̶
app.config(function($httpProvider) {
     $httpProvider.interceptors.push('myfactory');
})
app.factory('myfactory',  ['$q', '$location',function ($q, $location) {
    //..
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档