service returning result of a $ http

1

I'm creating a service for an AngularJS application where I should query a ws and return a value for a variable. My problem is that when I use $http I can not get this value to return, if I use a console.log() ... I do this:

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 callback function does not return the date or error now if I give a console.log(data) within .success or .error it returns ...

How can I send the return of this request to the external variable?

    
asked by anonymous 02.06.2015 / 21:02

4 answers

1

My solution was to use the Angular $ q function within my service. It instantiates a variable and sets a trigger for when there is a change, so I can get and make $http with its return into my var defer = $q.defer() variable and when I change it I continue my code.

(tip of Caio Felipe Pereira)

    
23.06.2015 / 16:24
1

Here I do something like this:

var variavel_global;
app.service('ajax', ['$http', function ($http){
    this.pesquisaCidade = function(q){
        return $http.get(url).then(function(response){
            return response;
        }, function(error){
            console.info('error: ' + error);
        });
    };
}]);
app.directive('pesquisa', ['ajax', function(ajax){
    return {
        link: function($scope){
            $scope.pesquisa = function (query){
                ajax.pesquisaCidade(query).then(function(resp){
                    console.info(resp);

                    //se quiser setar uma variavel
                    variavel_global = angular.copy(resp);
                });
            };
        }
    }
}]);

I think it gets more organized and easy to understand.

    
04.06.2015 / 23:28
1

If it is information that you will use in the global scope, I recommend declaring the variable in global scope, and within the function there assigning value to it:

$scope.retorno;

and there in function:

$scope.retorno = error;

or

$scope.retorno = data;
    
23.06.2015 / 15:19
1

But the self already returns a callback that may be returning from your service. The example that Jonatas replied here is a better way to work.

When you run a $ http it gives you some methods that run after the completion of the return. Within your controller (or where you use the service) you will execute and wait for its completion through the then () method.

Example:

app.service('api', function($http){
    var api = {};
    api.busca = function(){
        return $http.get('....').then(function(data) {
            return data;
        });
    };
    return api;
});

And in the controller:

app.controller('testeCtrl', function($scope, api) {
    $scope.resultados = undefined;
    api.busca().then(function(data){
        // executado após a conclusão da execução
        $scope.resultados = data;
    });
});

This is one of the ways you can separate the responsibilities of each module.

    
23.06.2015 / 16:53