javascript - AngularJS:在自定义服务中执行 $http 请求并返回数据

标签 javascript angularjs angularjs-service angular-http

我在 Angular 中定义了一个自定义的 http 服务,如下所示:

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;
});

我可以像这样在我的 Controller 中访问这个服务:

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

  var webService = 'getUser?u=3'

  myhttpserv.async(webService).then(function (data) {
      console.log(data);
  })

});

但是我现在需要简化这个过程,以便它全部包含在具有静态 url 的服务中,并且它只返回数据。这样我就可以像这样在 Controller 中调用它:

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

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

});

我似乎无法通过调整服务来获得此功能。有谁知道正确的做法吗?

最佳答案

选项 1 - 使用 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);
     });     
});

选项 2 - 使用路由解析

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

服务:

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...

});

选项 3 - 使用 $interval 服务

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);    
});

关于javascript - AngularJS:在自定义服务中执行 $http 请求并返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24126088/

相关文章:

angularjs - 模块如何访问另一个模块,即使它没有依赖关系

angularjs - 我想将日期格式设置为 MM/dd/yyyyTHH :MM:SS in Angularjs 2 and i want it should be only MM/DD/YYYY

javascript - 在AngularJS中的指令中使用服务函数

javascript - AngularJs:参数不是未定义的函数

javascript - 将 JSON 打印到屏幕以用于剪切和粘贴

javascript - Bootstrap 预滚动 DIV 的固定高度

javascript - jquery 函数在 safari 中无法工作,但在其他浏览器中可以正常工作

javascript - 记住在函数中使用的搜索查询

javascript - 在 WebGL 中批量调用的最快方法

angularjs - 后端 API 分组