1
Is there any way to run a code that runs before services?
Because I want to use the function $http.defaults.headers
before starting the services so that I can instantiate the header in all services I request, but it seems that it loads all the code and then returns it 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.