Kub*_*a T 3 javascript angularjs angular-http-interceptors
我想知道Angular.js $ http拦截器是否在整个应用程序中共享.
我们假设我有一个myDependentApp模块,在很多应用程序之间共享.该模块有一些拦截器配置为控制$ http请求/响应.我通过在应用程序引导程序中声明它来包含该模块:
angular.module('myApp', ['myDependentApp']);
Run Code Online (Sandbox Code Playgroud)
我有应用程序模板:
<html ng-app="myApp">
Run Code Online (Sandbox Code Playgroud)
是myDependentApp的拦截器会活跃myApp吗?
感谢帮助.
答案是肯定的,我在这里试了一下:
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');
}]);
Run Code Online (Sandbox Code Playgroud)
我看到请求被拦截了.这是小提琴:http://jsfiddle.net/6dbgo6pt/1/