simultaneous ajax requests angularjs?

前端 未结 2 713
再見小時候
再見小時候 2021-02-11 03:40

i want to send multipal ajax request at a time. this is my js code.

Re Check

app.control         


        
2条回答
  •  梦如初夏
    2021-02-11 04:19

    I rewrote your controller to use closures in order to make every call asynchronous and keep it DRY.

    I tested this and it works, check it out here: http://jsfiddle.net/s6uFx/2/

    app.controller('customersCtrl', function ($scope, $http, $timeout) {
      function cget(action) {
        return $http.get('cget.php', {params: {action: action}});
      }
    
      $scope.getDataajax = function () {
        cget('set_info')
        .success(function (res) {
            console.log(JSON.stringify(res));
        });
      };
    
      $scope.getEventSeconds = function () {
        cget('get_info')
        .success(function (res) {
          console.log(JSON.stringify(res));
          $scope.list = res;
          $scope.currentPage = 1; //current page
          $scope.entryLimit = 10; //max no of items to display in a page
          $scope.filteredItems = $scope.list.length; //Initially for no filter
          $scope.totalItems = $scope.list.length;
        })
        .then(function () {
          // and start to loop
          $timeout($scope.getEventSeconds, 5000);
        });
      };
    
      // this call replaces the first $http.get and starts the loop
      $scope.getEventSeconds();
    });
    

提交回复
热议问题