You can use promises to get an answer. it can be inside the service and call it whenever you want to use it.
this.addUser = function (obj) { var datosRecu = null; var deferred = $q.defer(); var uri = baseUrl + 'addUser'; $http({ url: uri, method: 'post', data: angular.toJson(obj) }).then(function successCallback(response) { datosRecu = response; deferred.resolve(datosRecu); }, function errorCallback(response) { datosRecu = response; deferred.resolve(datosRecu); }); return deferred.promise; };
Also .error and .success are deprecated.
PD: parameter data: inside $http matches the body. if you want to send parameters, you should use params:{}
EDIT: Here I will leave you a link how promises works. Angular promises This mainly helps to process data asynchronously
the above example can be used inside a service like this
myApp.service('myService', function($q, $http){
a service can be entered inside any controller to provide the data you want inside your functions.
myApp.controller('myController', function($scope, myService){ $scope.list = function(){ $promise = myService.getAll();
Hope this helps.
source share