使用$ q拒绝$ http Rerponse表单服务AngularJS

时间:2017-08-21 12:26:01

标签: angularjs angular-promise

在我的控制器中,我有30分钟的间隔服务电话。有一个服务的请求参数。如果在服务请求调用的时间跨度和响应之间更改了请求参数,我想取消响应。

通过在控制器中编写$ http调用,我可以使用$ q的解析方法解决它。但我必须为$ http服务维护一个单独的服务文件。

这是我的控制器:

app.controller('DCSubSystemCtrl', ['$scope', '$rootScope', '$localStorage', '$filter', '$interval', 'NgTableParams', 'operatorDCSubSysFactory', 'epochTime', '$timeout', 'operatorDashboardFactory',
  function($scope, $rootScope, $localStorage, $filter, $interval, NgTableParams, operatorDCSubSysFactory, epochTime, $timeout, operatorDashboardFactory) {
    $scope.perfoemanceGraph = function(blockID) {
      var reqObj = {
        "plantId": "IN.RJ.ABG.BHDL",
        "siteId": "IN.RJ.ABG.BHDL.Site1",
        "blockId": blockID,
        "subsystem": ""
      }
      operatorDashboardFactory.getWeatherFeed(reqObj).then(function(response) {
        if (response.status == 200) {
          $timeout($scope.perfoemanceGraph, 300 * 1000);
          $scope.weatherFeed = [];

          $scope.weatherFeed = response.data;
          console.log("Bye")
        } else {

        }
      });
    };
  }
]);
app.factory('operatorDCSubSysFactory', function($http, $log, $q, $resource, $location) {
  var baseUrl = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
  return {
    'getInverterBlocks': function(uriData) {
      ̶v̶a̶r̶ ̶d̶e̶f̶e̶r̶ ̶=̶ ̶$̶q̶.̶d̶e̶f̶e̶r̶(̶)̶;̶
      ͟r͟e͟t͟u͟r͟n͟ $http({
        headers: {
          'Content-Type': 'application/json'
        },
        data: uriData,
        method: 'POST',
        url: baseUrl + "/operator/dcsubsystem/inverterblock"
      }) ̶.̶t̶h̶e̶n̶(̶f̶u̶n̶c̶t̶i̶o̶n̶(̶s̶u̶c̶c̶e̶s̶s̶)̶ ̶{̶
        ̶d̶e̶f̶e̶r̶.̶r̶e̶s̶o̶l̶v̶e̶(̶s̶u̶c̶c̶e̶s̶s̶)̶;̶
      ̶}̶,̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶e̶r̶r̶o̶r̶)̶ ̶{̶
        ̶d̶e̶f̶e̶r̶.̶r̶e̶j̶e̶c̶t̶(̶e̶r̶r̶o̶r̶)̶;̶
      ̶}̶)̶;̶
      ̶r̶e̶t̶u̶r̶n̶ ̶d̶e̶f̶e̶r̶.̶p̶r̶o̶m̶i̶s̶e̶;̶
    };
  };
});

0 个答案:

没有答案