Javascript 使HTTP调用可重用

Javascript 使HTTP调用可重用,javascript,angularjs,Javascript,Angularjs,在我的controller.js中,我有一个HTTP GET请求,当我的页面加载和用户刷新时,我必须调用该请求 我现在的做法是复制$http代码。有没有一种方法可以使它更加可重用?我不知道如何将它移动到我的services.js文件中,并在我的控制器中引用它以使其运行 .controller('GamesCtrl', function ($scope, $http) { $http( { method: 'GET', url: 'https://www.kimon

在我的controller.js中,我有一个HTTP GET请求,当我的页面加载和用户刷新时,我必须调用该请求

我现在的做法是复制$http代码。有没有一种方法可以使它更加可重用?我不知道如何将它移动到我的services.js文件中,并在我的控制器中引用它以使其运行

.controller('GamesCtrl', function ($scope, $http) {
  $http(
  {
      method: 'GET',
      url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
      headers: {
          'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
      }
  }).
  success(function (data) {
      $scope.data = data['results']['collection1'];
  });

  $scope.doRefresh = function() {
    $http(
          {
              method: 'GET',
              url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
              headers: {
                  'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
              }
          }).
          success(function (data) {
              $scope.data = data['results']['collection1'];
          })
     .finally(function() {
       $scope.$broadcast('scroll.refreshComplete');
     });
  };
})

简单的答案是,创建一个私有函数:


.controller('GamesCtrl', function ($scope, $http) {
  var doLoad = function() {
      $http(
      {
          method: 'GET',
          url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
          headers: {
              'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
          }
      }).
      success(function (data) {
          $scope.data = data['results']['collection1'];
      }).finally(function() {
       $scope.$broadcast('scroll.refreshComplete');
     });;
  };

  $scope.doRefresh = function() {
    doLoad();
  };

  doLoad();
})
更详细的答案可能是将其提取到服务中:


.service('KimonoSvc',function($http) {
    return {
      get: function() {
         return $http(
         {
             method: 'GET',
             url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
             headers: {
                 'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
             }
         });
      }
    };
})
.controller('GamesCtrl', function ($scope, KimonoSvc) {
  var doGet = function() {
    KimonoSvc.get().
         success(function (data) {
             $scope.data = data['results']['collection1'];
         }).finally(function() {
          $scope.$broadcast('scroll.refreshComplete');
        });
  };
  $scope.doRefresh = function() {
    doGet();
  };

  doGet();
})

简单的答案是,创建一个私有函数:


.controller('GamesCtrl', function ($scope, $http) {
  var doLoad = function() {
      $http(
      {
          method: 'GET',
          url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
          headers: {
              'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
          }
      }).
      success(function (data) {
          $scope.data = data['results']['collection1'];
      }).finally(function() {
       $scope.$broadcast('scroll.refreshComplete');
     });;
  };

  $scope.doRefresh = function() {
    doLoad();
  };

  doLoad();
})
更详细的答案可能是将其提取到服务中:


.service('KimonoSvc',function($http) {
    return {
      get: function() {
         return $http(
         {
             method: 'GET',
             url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
             headers: {
                 'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
             }
         });
      }
    };
})
.controller('GamesCtrl', function ($scope, KimonoSvc) {
  var doGet = function() {
    KimonoSvc.get().
         success(function (data) {
             $scope.data = data['results']['collection1'];
         }).finally(function() {
          $scope.$broadcast('scroll.refreshComplete');
        });
  };
  $scope.doRefresh = function() {
    doGet();
  };

  doGet();
})

简单的答案是,创建一个私有函数:


.controller('GamesCtrl', function ($scope, $http) {
  var doLoad = function() {
      $http(
      {
          method: 'GET',
          url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
          headers: {
              'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
          }
      }).
      success(function (data) {
          $scope.data = data['results']['collection1'];
      }).finally(function() {
       $scope.$broadcast('scroll.refreshComplete');
     });;
  };

  $scope.doRefresh = function() {
    doLoad();
  };

  doLoad();
})
更详细的答案可能是将其提取到服务中:


.service('KimonoSvc',function($http) {
    return {
      get: function() {
         return $http(
         {
             method: 'GET',
             url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
             headers: {
                 'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
             }
         });
      }
    };
})
.controller('GamesCtrl', function ($scope, KimonoSvc) {
  var doGet = function() {
    KimonoSvc.get().
         success(function (data) {
             $scope.data = data['results']['collection1'];
         }).finally(function() {
          $scope.$broadcast('scroll.refreshComplete');
        });
  };
  $scope.doRefresh = function() {
    doGet();
  };

  doGet();
})

简单的答案是,创建一个私有函数:


.controller('GamesCtrl', function ($scope, $http) {
  var doLoad = function() {
      $http(
      {
          method: 'GET',
          url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
          headers: {
              'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
          }
      }).
      success(function (data) {
          $scope.data = data['results']['collection1'];
      }).finally(function() {
       $scope.$broadcast('scroll.refreshComplete');
     });;
  };

  $scope.doRefresh = function() {
    doLoad();
  };

  doLoad();
})
更详细的答案可能是将其提取到服务中:


.service('KimonoSvc',function($http) {
    return {
      get: function() {
         return $http(
         {
             method: 'GET',
             url: 'https://www.kimonolabs.com/api/dc6n4edu?apikey=U4SNiysE89aaLXSWRJgHKDZOByqSLM0p',
             headers: {
                 'authorization': 'Bearer PW8V4OesZ61tCqSRNpXYtRn5ahcLdclU'
             }
         });
      }
    };
})
.controller('GamesCtrl', function ($scope, KimonoSvc) {
  var doGet = function() {
    KimonoSvc.get().
         success(function (data) {
             $scope.data = data['results']['collection1'];
         }).finally(function() {
          $scope.$broadcast('scroll.refreshComplete');
        });
  };
  $scope.doRefresh = function() {
    doGet();
  };

  doGet();
})


使用服务或工厂。如果您将服务模块与主模块分开,请不要忘记将它们添加到主模块中。还将服务(例如UserService)注入控制器中。如何在控制器中引用服务或工厂?我需要吗?使用服务或工厂。如果您将服务模块与主模块分开,请不要忘记将它们添加到主模块中。还将服务(例如UserService)注入控制器中。如何在控制器中引用服务或工厂?我需要吗?使用服务或工厂。如果您将服务模块与主模块分开,请不要忘记将它们添加到主模块中。还将服务(例如UserService)注入控制器中。如何在控制器中引用服务或工厂?我需要吗?使用服务或工厂。如果您将服务模块与主模块分开,请不要忘记将它们添加到主模块中。还将服务(例如UserService)注入控制器中。如何在控制器中引用服务或工厂?我需要吗?
$scope.doRefresh=doLoad
$scope.doRefresh=doGet。不需要额外的匿名函数。是的,我想你可以这样做,然后在控制器的末尾调用
$scope.doRefresh()
。我喜欢单独的功能,风格上(命名,而不是匿名)。不,你误解了我。。创建函数很好。。只是不要使用另一个将其应用于
doRefresh
<代码>$scope.doRefresh=function(){doGet();}可能变成
$scope.doRefresh=doGet$scope.doRefresh=doGet
$scope.doRefresh=doLoad
$scope.doRefresh=doGet。不需要额外的匿名函数。是的,我想你可以这样做,然后在控制器的末尾调用
$scope.doRefresh()
。我喜欢单独的功能,风格上(命名,而不是匿名)。不,你误解了我。。创建函数很好。。只是不要使用另一个将其应用于
doRefresh
<代码>$scope.doRefresh=function(){doGet();}可能变成
$scope.doRefresh=doGet$scope.doRefresh=doGet
$scope.doRefresh=doLoad
$scope.doRefresh=doGet。不需要额外的匿名函数。是的,我想你可以这样做,然后在控制器的末尾调用
$scope.doRefresh()
。我喜欢单独的功能,风格上(命名,而不是匿名)。不,你误解了我。。创建函数很好。。只是不要使用另一个将其应用于
doRefresh
<代码>$scope.doRefresh=function(){doGet();}可能变成
$scope.doRefresh=doGet$scope.doRefresh=doGet
$scope.doRefresh=doLoad
$scope.doRefresh=doGet。不需要额外的匿名函数。是的,我想你可以这样做,然后在控制器的末尾调用
$scope.doRefresh()
。我喜欢单独的功能,风格上(命名,而不是匿名)。不,你误解了我。。创建函数很好。。只是不要使用另一个将其应用于
doRefresh
<代码>$scope.doRefresh=function(){doGet();}可能变成
$scope.doRefresh=doGet$scope.doRefresh=doGet