【问题标题】:Http response getting overridden by previous call responseHttp 响应被先前的调用响应覆盖
【发布时间】:2016-02-13 12:27:11
【问题描述】:

我列出了每个带有复选框的列表。选择复选框会导致 3 个不同的休息调用,并且响应与该列表的 HTML 绑定。如果用户点击多个复选框真的很快,那么后面调用的响应会覆盖前面调用的响应。(前面的调用仍在播放响应,后面的调用已经覆盖了前面的调用)

例如,如果我真的很快地检查了 6 个复选框,那么对于其余的一些调用,响应可能不会被执行。

getRequestRecords: function(obj) {
                return $http({
                    url: $serverPath + '/alumCenter/notifications/request/' +obj',
                    method: "GET"
                });
            },
$scope.singleSelectSet=function(obj){
   myService.getRequestRecords(obj).then(function(response){
       $scope.myVar=response;
          // do lots of things
        });
}

<div ng-repeat="obj in flats track by $index">
<input type="checkbox" class="selectable" ng-click="singleSelectSet(obj)" ng-checked="showChecked($index)" aria-label="{{:: 'common.slideout.aria.slatSelectCheckboxLabel' | i18n }}">
</div>

【问题讨论】:

  • 这可能和这个问题一样:stackoverflow.com/questions/35375120/…
  • @ThomasGhesquiere:我不想取消其余的通话。在我的情况下这将是错误的。该复选框已被选中。如果其对应的数据不可见,将是不好的用户体验。
  • 我们不知道如何使用getRequestRecords()。显示所有相关代码
  • @charlietfl : 请查看新编辑
  • 显示的内容未连接到任何复选框。

标签: javascript angularjs http


【解决方案1】:

是否可以选择使用数组并将响应推送到它上面?这样您就不会覆盖 $scope 中的变量。

$scope.myVar = [];

myService.getRequestRecords().then(function(response){
    $scope.myVar.push(response);
    // do lots of things
});

根据我的评论,$q.all 的使用示例:

// on check, wait for your three calls to be complete
// before triggering the callback
$scope.singleSelectSet=function(obj){
    var queue = [
        myService.getRequestRecords(),
        myService.getOtherRequest(),
        myService.getLastRequest()
    ];

    $q.all(queue).then(function(response){
        var responseRequestRecords = response[0],
            responseOtherRequest = response[1],
            responseLastRequest = response[2];
        // do lots of things
    });
}

记得在你的控制器中注入 $q。

【讨论】:

  • 此响应后有 3 个不同的休息调用。我无法将其存储在数组中。有没有更流畅的方法?
  • @RahulB 您可以将您的 3 个休息呼叫与 $q.all() 组合为一个,并在完成后更新您的 $scope.myVar。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 2018-08-15
  • 1970-01-01
  • 2023-02-24
相关资源
最近更新 更多