2016-02-13 80 views
-1

我有每个都带有复选框的列表。复选框上的选择会导致3个不同的其他调用,并且响应会与该列表的HTML绑定。如果用户点击多个复选框的速度非常快,那么后者的响应将覆盖之前的响应(先前的响应仍在响应中,而后者的响应已覆盖上一个响应)通过先前的呼叫响应覆盖Http响应

Eg-如果我选中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> 
+0

这可能是这样的问题:http://stackoverflow.com/questions/35375120/cancelling-ongoing-angular-http-requests-when-theres-a-new-request –

+0

@ThomasGhesquiere:我不想要取消剩余的通话。对我而言这将是错误的。该复选框已被选中。如果其相应的数据不可见,那将是不好的用户体验。 – RahulB

+0

我们不知道如何使用'getRequestRecords()'。显示所有相关代码 – charlietfl

回答

0

是否可以使用数组并将响应推送到它上面?这样你不会覆盖$ 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。

+0

此回复后有3个不同的其他呼叫。我无法将其存储在数组中。有没有更平滑的方式来做到这一点? – RahulB

+0

@RahulB你可以将你的3个休息调用与[$ q.all()](https://docs.angularjs.org/api/ng/service/$q#all)分组,并更新你的$ scope.myVar当完成时。 –

+0

嗯.. $ q.all()。让我读一下 – RahulB