Is there a way to run a code that runs before services?
Because I want to use the $http.defaults.headers
function before starting the services so that I can instantiate the header on all services that I request, but it seems like it loads all the code and then returns that to me.
The function that does this is below:
function get(url) {
var deferred = $q.defer();
$http.post(url,{'device_id':testedevice})
.success(function (data) {
deferred.resolve(data);
})
.error(function (error) {
deferred.reject(error);
});
return deferred.promise;
}
get(base_url+"/api/access/token")
.then(function (data) {
if(!localStorage.getItem("access_token")){
localStorage.setItem("access_token",data.token);
$rootScope.keyUser = data.token;
}else{
var userToken = localStorage.getItem("access_token");
$rootScope.keyUser = userToken;
}
})
.finally(function(){
$rootScope.on(function(){
$http.defaults.headers.get = {'access_token': userToken,'device_id':testedevice}
});
});
PS: The header is applied, but after the services have already been called.