简体   繁体   English

AngularJS如何防止重复的http请求?

[英]AngularJS How to prevent duplicated http requests?

I'm struggling for the past day with some weird situation. 我在过去的一天中遇到了一些奇怪的情况。 What's happening is that for an http request to an API on a remote server occasionally duplicate requests are being sent. 发生的事情是,对于远程服务器上的API的http请求,偶尔会发送重复请求。 Can anyone please provide help on how to avoid these duplicated requests? 任何人都可以提供有关如何避免这些重复请求的帮助吗?

Here is an example of a function that I use on a factory: 以下是我在工厂中使用的函数示例:

factory.getAllConsultedClientsLogs = function(oParams) {

    var deferred = $q.defer();

    $http.post('url/to/api', oParams)
        .success(function(response) {
            deferred.resolve(response);
        })
        .error(function() {
            deferred.reject("Error! @factory.getAllConsultedClientsLogs");
        });

return deferred.promise;

};

...and an example of a function using the above indicated on a controller: ...以及在控制器上使用上述指示的功能示例:

$scope.openConsultedClientsLogsModal = function(operator, date) {

    if (angular.isDefined(operator) && angular.isDefined(date)) {

        RrAuditingFactory.getAllConsultedClientsLogs({'operator':operator,'date':date}).then(function(promise) {

            if (angular.isObject(promise) && angular.isDefined(promise.error) && promise.error == 0) {

                var modalInstance = $modal.open({
                    templateUrl: 'path/partial',
                    controller: function($scope, $modalInstance, logsResult) {
                        $scope.logsResult = logsResult;
                    },
                    resolve: {
                        logsResult: function() {
                            return promise.result;
                        }
                    }
                });

                modalInstance.result.then(function() {
                }, function () {
                });

            } else {
                ErrorContext.setError(promise.errorMsg);
            }

        }, function(promise) {
            ErrorContext.setError(promise);
        });

    } else {

        ErrorContext.setError();

    }

};

Thank you in advance.. hope that anyone could help me out... 提前谢谢..希望有人能帮帮我...

i have faced this problem, and you can resolve it like this : 我遇到过这个问题,你可以这样解决:

check if you have declared ng-controller twice , you need to declare it just one time check if you have declared data-ng-click , if so , you need to replace it with ng-click that's it 检查你是否已经声明ng-controller两次,你需要声明它只是一次检查你是否已声明data-ng-click,如果是,你需要用ng-click替换它就是它

I saw your link: 我看到了你的链接:

  $scope.fnRowCallback = function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { $('td:eq(4)', nRow).bind('click', function() { $scope.$apply(function() { $scope.openConsultedClientsLogsModal(aData.Operator, aData.LogDate); }); }); return nRow; }; 

You can unbind before doing the bind, this way you will prevent duplicates. 您可以在执行绑定之前取消绑定,这样可以防止重复。 Try like this: 试试这样:

    $scope.fnRowCallback = function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {



        //add this unbind to your code
        $('td:eq(4)', nRow).unbind("click");


        $('td:eq(4)', nRow).bind('click', function() {
            $scope.$apply(function() {
                $scope.openConsultedClientsLogsModal(aData.Operator, aData.LogDate);
            });
        });

        return nRow;
    };

I hope this helps. 我希望这有帮助。

I tend to return an object from a factory, so in your case I'd do something like: 我倾向于从工厂返回一个物体,所以在你的情况下我会做类似的事情:

module.factory('clientsLogs', function($q, $http) {

  return {
    getAllConsulted: function(oParams) {
      var deferred = $q.defer();
      $http.post('url/to/api', oParams)
        .then(function(response) {
            deferred.resolve(response);
        }, function() {
            deferred.reject("Error! @factory.getAllConsultedClientsLogs");
        });

      return deferred.promise;
    }
  }

});

and then in your controller something like: 然后在你的控制器中,例如:

module.controller('MyCtrl', function($scope, clientsLogs) {
  clientLogs.getAllConsulted({}).then(function(){...})
}

Might not help you but I've never had any problems with duplicate calls doing it this way. 可能没有帮助你,但我从来没有遇到任何重复调用这样做的问题。

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

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