简体   繁体   English

无法通过angularjs中的一项服务列出HTTP get调用列表来处理异常?

[英]Unable to make a list of http get calls through one service in angularjs to handle exceptions?

Process: I try to call 4 httpget call though one httpGet method in a service, my first httpget call return an error and the rest of the calls return success message. 流程:我尝试通过服务中的一个httpGet方法调用4个httpget调用,我的第一个httpget调用返回一个错误,其余的调用返回成功消息。

issues: I try to call that error function response to my controller but through console error TypeError:errorFunction is not a function 问题:我尝试调用该错误函数对控制器的响应,但通过控制台错误TypeError:errorFunction不是函数

在此处输入图片说明

Code: 码:

myController: myController:

app.controller('myCtrl',["myService",function(myService){
     var id =10;
     $scope.getTemp = function () {
          myService.getTemplate(id, $scope.getTemplateResponse, $scope.ErrorResponse);
     };
      $scope.getTemplateResponse= function (response) {
              $scope.bodyTemplate = response.Value;
      };
      $scope.ErrorResponse = function(error){
             window.location.href = "Error#?" + error;
      }
      $scope.getTemp ();
}]);

myService: myService:

app.service("myService", ["ajaxService", function (ajaxService) {
      this.getTemplate= function (id, successFunction, errorFunction) {
               ajaxService.Get("http://gettempaltes.com/api/id="+id, successFunction, errorFunction);
       };
}]);

ajaxService: ajaxService:

app.service("ajaxService", ["$http", function($http) {
      this.Get= function (route, successFunction, errorFunction) {
            $http.get(route).success(function (response, status) {
                successFunction(response, status);
            }).error(function(response) {
                errorFunction(response);
            });
       };
}]);

Define $scope.ErrorResponse before you point to it. 在指向$ scope.ErrorResponse之前,先对其进行定义。 Like this 像这样

$scope.ErrorResponse = function(error)
{
  window.location.href = "Error#?" + error;
}

$scope.getTemp = function () 
{
  myService.getTemplate(id, $scope.getTemplateResponse, $scope.ErrorResponse);
};

Validate success/error fn's have been passed in before using them. 在使用前先验证成功/错误信息是否已通过。 Like this 像这样

app.service("ajaxService", ["$http", function($http) 
{
  this.Get= function (route, successFunction, errorFunction) 
  {
    $http.get(route)
      .success(function (response, status) 
      {
        if(angular.isFunction(successFunction))
        {
          successFunction(response, status);
        } 
      })
      .error(function(response) 
      {
        if(angular.isFunction(errorFunction))
        {
          errorFunction(response);
        }
      });
  };
}]);

If you are using promise then change your service like this: 如果您使用的是Promise,请按以下方式更改服务:

 getUserDetail: function (id) {
    var deferred = $q.defer();

    var user = $http({
        url: 'api/user/GetByUserId',
        method: "POST",
        params: { 'userId': id }
    });

    var userDetail = $http({
        url: 'api/user/GetUserDetail',
        method: "GET",
        params: { 'userId': id }
    });

    let promises = {
        info: user,
        detail: userDetail
    }

    $q.all(promises).then(function (response) {
        this.data.user = response.info.data;
        this.data.detail = response.detail.data;
        deferred.resolve(response);
    });
    return deferred.promise;
}

In controller: 在控制器中:

ServiceName.getUserDetail(Id).then(function (data) {
    $scope.user = ServiceName.data.user;
    $scope.detail = ServiceName.data.detail;
}, function (error) {
        $scope.error = error.Message;
});

In this way we can queue up as much as we want, if any of them throw error, in controller error function will be called. 这样,我们可以根据需要排队,如果其中任何一个抛出错误,则将调用控制器错误功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM