Ich versuche zu verstehen, wie in Ansible 1.5.3 ein httpInterceptor zu schreiben.Angular - HttpInterceptor + Async Anruf
auf 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');
}
Jetzt Basierend Ich verstehe nicht, wie MyService zu schreiben (es führt eine GET).
Ich habe versucht, in myconfig Funktion hinzuzufügen, aber ich bin in zirkulärer Abhängigkeit verloren:
$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;
})
Kann mir jemand helfen?
Riccardo