Angularjs Angular.js代码,带有$httpProvider和promise。它有什么作用?
您能否大致解释一下该代码的作用:Angularjs Angular.js代码,带有$httpProvider和promise。它有什么作用?,angularjs,httpresponse,interceptor,promise,Angularjs,Httpresponse,Interceptor,Promise,您能否大致解释一下该代码的作用: App.config(['$httpProvider', function ($httpProvider) { $httpProvider.responseInterceptors.push('HttpSpinnerInterceptor'); $httpProvider.defaults.transformRequest.push(function (data, headersGetter) { angular.element('.brand
App.config(['$httpProvider', function ($httpProvider) {
$httpProvider.responseInterceptors.push('HttpSpinnerInterceptor');
$httpProvider.defaults.transformRequest.push(function (data, headersGetter) {
angular.element('.brand img').attr("src","<%= asset_path('brand/brand.gif') %>");
return data;
});
}]);
App.factory('HttpSpinnerInterceptor', function ($q, $window) {
return function (promise) {
return promise.then(function (response) {
angular.element('.brand img').attr("src","<%= asset_path('brand/brand.png') %>");
return response;
}, function (response) {
angular.element('.brand img').attr("src","<%= asset_path('brand/brand.png') %>");
return $q.reject(response);
});
};
});
App.config(['$httpProvider',函数($httpProvider){
$httpProvider.responseInterceptors.push('HttpSpinnerInterceptor');
$httpProvider.defaults.transformRequest.push(函数(数据,HeaderGetter){
angular.element('.brand img').attr(“src”,”);
返回数据;
});
}]);
应用程序工厂('HttpSpinnerInterceptor',函数($q,$window){
返回函数(承诺){
返回承诺。然后(函数(响应){
angular.element('.brand img').attr(“src”,”);
返回响应;
},功能(回应){
angular.element('.brand img').attr(“src”,”);
返回$q.reject(响应);
});
};
});
我完全不理解,只是猜测它截获了一些响应并注入了image的src属性
我不明白HttpSpinnerInterceptor是如何和何时被调用的,以及“promise”参数是什么
$http
服务发出的每个请求完成(成功或失败)后调用,但在向调用方解析承诺之前调用(所以您可以推迟结果)。实际上,不需要转换请求,因为它与HttpSpinnerInterceptor(或者不需要HttpSpinnerInterceptor…)基本相同,因为它不转换任何东西promise
参数是一个$q
promise,当您需要对请求的结果执行一些异步操作时,可以使用它,因为您可以稍后解析请求,以便调用方稍后获得结果。实际上,在代码中,您可以直接解析(或拒绝)这个承诺,更改图像的src属性$http
服务:-仔细查看“响应拦截器”和“转换请求和响应”非常感谢您的解释和链接!