Good, my doubt is this, I am trying to get the data
from a api
that is in url
, I use $http.get
, I assign it to $scope.ips
and it shows me the data in console.log()
respective, now the problem is that $http.get
is a promise so it returns the data once they are available, but since I would like to do the following print the data out of $http.get
, as I could do, then I try to do it and I get the empty object, I know it is a simple example, but I have been in other projects to consult data of api
, and from the data of that api
, consult again data in it forming an endless spaghetti code .
var app=angular.module("app",[]);
app.controller("main",function($scope,$http){
$scope.ips={};
var url="http://ip-api.com/json/208.80.152.201";
$http.get(url)
.success(function(data){
$scope.ips=data;
console.log($scope.ips)
});
console.log($scope.ips);
})
For example, if you would like to do something else within the same query, so on as you see the size of the code increasing as you could modularize it.
var app=angular.module("app",[]);
app.controller("main",function($scope,$http){
$scope.ips={};
var url="http://ip-api.com/json/208.80.152.201";
$http.get(url)
.success(function(data){
$scope.ips=data;
console.log($scope.ips)
var url2="XXXXXXXXXXXXXXXXX"+data.city;
$http.get(url2)
...
....
});
console.log($scope.ips);
})