introduction: I have a problem return value in $http method, i understand how to works $http in $http.post and $http.get method, i can't return value outside the function that call to my factory.
i need use $scope.historias value outside the controller function.
My Angular Factory: First Factory:
mediperbaricaApp.factory('listarTratamientos',['$http','$location','$q',
function($http, $location, $q){
var urlBase = host + 'index.php/';
var service = {};
console.log('DEBUG = entramos');
return {
response : function(response){
return $http({
url : (urlBase + 'tratamientos/getTratamientos'),
method : 'GET'
})
}
}
}]);
second Factory:
mediperbaricaApp.factory('services', [ '$http' ,'$location', function(
$http, $location){
var urlBase = host + 'index.php/';
var service = {};
//listado de historias para el autocoplete del form
service.getHistorias = function(callback){
console.log('[DEBUG] FAC Listar Historias');
$http.get(urlBase + 'historias/getHistoria').
success(function(response){
return callback(response);
}).
error(function(response){
alert('Lo sentimos!, No se puede recuperar los datos, ' +
' intente mas tarde :(');
})
};
return service;
}]);
My Angular Controller Controller, using two last factories, i get same result
mediperbaricaApp.controller('editController',function($scope,listarTratamientos,
$location,$routeParams, services){
console.log('[DEBUG] CTRL editar tratamientos');
//use first factory
$scope.datos = {};
services.getHistorias(function(response){
$scope.datos = response.data; // output array with data :)
})
//the data no exist in this place.
console.dir($scope.datos); //$scope.datos lost data :(
//use second factory
$scope.midata = {};
listarTratamientos.response().success(function(data){
$scope.midata = data;
console.dir($scope.midata); // output array with data :)
});
//data not exist in this place i dont understand
console.dir($scope.midata); //$scope.datos lost data :(
Thanks your help!. Att. Eduardo
console.log(or.dir) is executed immediately, it doesn't wait for a promise to be fulfilled. Once the promise finishes, the data will be there, but it's not there when you execute the command.