Reputation: 183
i've this factory thar call external api that return an array:
angular.module('starter.services', [])
.factory('PlacesService', function($http) {
var places = "";
var request = $http({
method: "get",
url: 'http://someurl/getPlaces.php'
});
request.then(function (data) {
places = data.response
});
console.log(places); // return empty string
return {
all: function() {
return places;
},
get: function(placesId) {
return places[placesId];
}
}
});
Places variable returned by http is an empty string. If I initialize places as an array and the I use places.push(data.response) it works, but return an array of array. Could you help me?
Upvotes: 2
Views: 404
Reputation: 301
Your log here is outside of the context of the promise the $http
call is returning. Basically the log gets executed before the promise is resolved, so your string is still empty when the log executes.
Upvotes: 2