使用angular操纵异步调用

时间:2015-11-29 00:12:42

标签: javascript angularjs mongodb

我有一个显示MongoDB事件的日历。 每次用户点击prev或下一个按钮时,我都会进行异步调用,而不是重置当前的日期和事件数组;但如果用户多次点击某个按钮会显示双倍结果。

 $scope.next = function () {
     //reset array
    $scope.gridOptions.data=[];

    //date of week

    myService.dbAction(dt3,dt4).then(function (data) {

      check(data);
      $scope.gridOptions.data = $scope.setts;//update data
    }, function (error) {
      console.error('ERROR: ' + error);
    })
 }

我只想要上周的活动,怎么做?

1 个答案:

答案 0 :(得分:0)

快速解决方法是添加fetchPending标志。使用它来避免重复提取。

$scope.next = function () {
    if ($scope.fetchPending) return;
    $scope.fetchPending = true;
    //reset array
    $scope.gridOptions.data=[];

    //date of week

    myService.dbAction(dt3,dt4).then(function (data) {

      check(data);
      $scope.gridOptions.data = $scope.setts;//update data

    }).catch ( function (error) {
       console.error('ERROR: ' + error);
    }).finally( function () {
       $scope.fetchPending = false;
    })
 }