1
I am creating a service for an application Angularjs where I should query a Ws and return a value to a variable. My problem is that when I use the $http
I can’t get that amount back if I use a console.log()
... I do so:
app.service('wsService', function($http){
var callback;
$http.get('http://www.meudominio.com/ws')
.success(function (data) {
callback = data;
})
.error(function (d, s) {
callback = 'error';
})
;
//retorne o callback
return callback;
});
my problem is that if I do this my job callback does not return the date or the error now if I give a console.log(data)
within the .success
or .error
he returns...
how can I send the return of this request to the external variable?
Even creating a Global variable? var Valor; at the beginning and Value = date within the callback. Return Value
– Diego Souza
tries to declare
callback
within the.success
and of.error
, guyvar callback = data;
– Caio Felipe Pereira
@Diegosouza I create a global variable at the beginning of the service, it is defined there as
callback
but even so the return does not work...– LeandroLuk
@Caiofelipepereira try to do as you said, but if I declare internally I am creating a new variable but with its scope only inside the ajax... I had thought about it before but it didn’t work...
– LeandroLuk
is that as ajax is asynchronous, it can cause this kind of problem. maybe set the global
async
asfalse
resolve– Caio Felipe Pereira
@Caiofelipepereira could show me an example of where I configure this in Angularjs?
– LeandroLuk
http://stackoverflow.com/questions/13088153/how-to-http-synchronous-call-with-angularjs take a look at this discussion. my suggestion, at the end of the day, is not that simple
– Caio Felipe Pereira