I use cordova
and ionic
for my mobile application.
I trying to use $http.get()
for loading a JSON
web service, so I wrote this code :
.controller('loading', function ($scope, $state, $http) {
var loadedService = {
news: false,
users: false
};
$http.get('http://{url}/users')
.success(function (result) {
loadedService.users = result;
});
I want to change the loadedService.users
to returned result
from webservice.
But when I trying to console.log(loadedService.users)
I will get false
(the default value for this variable).
What should I do?
$http.get, or javascript in general is asynchronous. Put console.log inside the success callback of http.get, you will see that (hopefully), you are getting correct results there. But if you put console.log after the get call, you will see the false output since your get call is still busy but your js keeps running. Long story short, js is asynchronous.