1
나는 Anipal 1.5.3에서 httpInterceptor를 작성하는 방법을 이해하려고합니다. 각도 - HttpInterceptor + 비동기 호출
지금 나는 (그것이 GET을 실행)이면 MyService를 작성하는 방법을 이해하지 http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/(function() {
'use strict';
angular
.module('app', ['ui.router', 'ngStorage', 'angular-loading-bar', 'angular-jwt'])
.config(myconfig)
.run(myrun);
function myconfig($stateProvider, $urlRouterProvider, $provide, $httpProvider) {
...
$provide.factory('httpRequestInterceptor', ['$q', 'MyService', function($q, MyService) {
return {
'request': function(config) {
var deferred = $q.defer();
MyService.execService().then(function(mydata) {
// Asynchronous operation succeeded, modify config accordingly
console.log("Async OK")
deferred.resolve(config);
}, function() {
// Asynchronous operation failed, modify config accordingly
console.log("Async KO")
deferred.resolve(config);
});
return deferred.promise;
}
};
}]);
$httpProvider.interceptors.push('httpRequestInterceptor');
}
기준으로합니다.
내가 MyConfig를 기능에 추가하려고하지만 순환 종속성에서 잃었어요 :
이$provide.factory('MyService', function($injector) {
var MyService = {
async: function() {
// $http returns a promise, which has a then function, which also returns a promise
console.log("CIAO")
var $http = $injector.get('$http');
var promise = $http.get('refresh_token.json').then(function (response) {
// The then function here is an opportunity to modify the response
console.log(response);
// The return value gets picked up by the then in the controller.
return response.data;
});
// Return the promise to the controller
return promise;
}
};
return MyService;
})
누군가가 나를 도울 수 있습니까?
리카르도