共享的AngularJS $ http拦截器 [英] Shared AngularJS $http interceptors

查看:77
本文介绍了共享的AngularJS $ http拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道整个应用程序是否共享Angular.js $ http拦截器.
假设我有一个myDependentApp模块,在许多应用程序之间共享.该模块具有一些配置为控制$ http请求/响应的拦截器.我通过在应用程序引导程序中声明该模块来包括该模块:

I am wondering if Angular.js $http interceptors are shared thoughout the whole application.
Let's assume I have a myDependentApp module, shared between many apps. That module has some interceptor configured to control the $http requests/responses. I include that module by declaring it in application bootstrap:

angular.module('myApp', ['myDependentApp']);

我有应用程序模板:

<html ng-app="myApp">

myDependentApp的拦截器是否在myApp中处于活动状态?

Are myDependentApp's interceptors going to be active in myApp?

感谢您的帮助.

推荐答案

是的,我在这里尝试过:

The answer is yes, I tried it here:

var dependentApp = angular.module('dependency',[]).config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push(function ($q) {
        return {
            'request': function (config) {
                console.log('request intercept');
            },

                'response': function (response) {
                console.log('response intercept');
            }
        };
    });
}]);

var app = angular.module('myapp', ['dependency']);
app.controller('mycontroller', ['$scope', '$http', function ($scope, $http) {
    $http.get('http://www.google.com');
}]);

我看到请求被拦截了.这是小提琴: http://jsfiddle.net/6dbgo6pt/1/

And I saw that the request was being intercepted. Here's the fiddle: http://jsfiddle.net/6dbgo6pt/1/

这篇关于共享的AngularJS $ http拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆