AngularJS: Performing $http request inside custom service and returning data

19,862

Option 1 - Use promise API

angular.module('myApp').factory('myhttpserv', function ($http) {
  return $http.get('http://my.ip.address/getUser?u=3', { cache: true });
});

Controller:

angular.module('myApp').controller('myCtrl', function ($scope, myhttpserv) {
     myhttpserv.then(function(response){
         console.log(response.data);
     });     
});

Option 2 - Using route resolve

angular.module('myApp', ['ngRoute']).config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/myCtrl', {
        templateUrl: 'myView.html',
        controller: 'myCtrl',
        resolve: {
        load: function (myhttpserv) {
            return myhttpserv;
        }
      });
  }]);

Service:

angular.module('myApp').factory('myhttpserv', function ($http) {
      var data = {};
      var url = "http://my.ip.address/";
      var promise = $http.get(url + 'getUser?u=3', { cache: true }).then(function (response) {
                data = response.data;
            });
      return data;
    });

Controller:

 angular.module('myApp')
  .controller('myCtrl', function ($scope, myhttpserv) {

      console.log(myhttpserv.data.var1);
      console.log(myhttpserv.data.var1);
      etc...

});

Option 3 - Use $interval service

angular.module('myApp').factory('myhttpserv', function ($http) {
  var data = {};
  var url = "http://my.ip.address/";
  var promise = $http.get(url + 'getUser?u=3', { cache: true }).then(function (response) {
            data = response.data;
        });
  return data;
});

Controller:

angular.module('myApp').controller('myCtrl', function ($scope, $interval, myhttpserv) {
      $scope.intervalPromise = $interval(function(){
          if (Object.keys(myhttpserv.data).length!=0)
          {
              console.log(myhttpserv.data);
              $interval.cancel($scope.intervalPromise);
          }
      }, 100);    
});
Share:
19,862
aadu
Author by

aadu

Updated on July 24, 2022

Comments

  • aadu
    aadu almost 2 years

    I have defined a custom http service in angular that looks like this:

    angular.module('myApp')
      .factory('myhttpserv', function ($http) {
    
      var url = "http://my.ip.address/"
    
      var http = {
          async: function (webService) {
              var promise = $http.get(url + webService, { cache: true }).then(function (response) {
                return response.data;
            });
              return promise;
           }
      };
      return http;
    });
    

    And I can access this service in my controller like so:

    angular.module('myApp')
      .controller('myCtrl', function (myhttpserv) {
    
      var webService = 'getUser?u=3'
    
      myhttpserv.async(webService).then(function (data) {
          console.log(data);
      })
    
    });
    

    However I now need to streamline this process so that it is ALL contained inside the service with a static url and it simply returns the data. So that I can just call it in the controller like so:

     angular.module('myApp')
      .controller('myCtrl', function ($scope, myhttpserv) {
    
          console.log(myhttpserv.var1);
          console.log(myhttpserv.var2);
          etc...
    
    });
    

    I can't seem to tweak the service to get this functionality. Anyone know the correct way to do it?