1
我想了解如何在Ansible 1.5.3中編寫一個httpInterceptor。基於http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/Angular - HttpInterceptor +異步調用
(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');
}
現在我不知道怎麼寫的MyService(執行一個GET)。
我試圖在的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;
})
有人能幫助我嗎?
Riccardo