简体   繁体   English

用angular操作异步呼叫

[英]Manipulate async call with angular

I have a calendar that displays events from MongoDB. 我有一个日历,显示来自MongoDB的事件。 Every time a user click on prev or next button, i do an asynchronous call and than i reset the current array of days and events; 每当用户单击上一个或下一个按钮时,我都会进行一次异步调用,然后重置当前的日期和事件数组; but if a user clicks on a button several times are shown double results. 但是如果用户多次单击按钮,则会显示两次结果。

 $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);
    })
 }

I want only the last week events , how to do that? 我只想要上周的活动,该怎么办?

A quick fix is to add a fetchPending flag. 一种快速解决方案是添加fetchPending标志。 Use it to avoid duplicate fetches. 用它来避免重复获取。

$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;
    })
 }

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

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