2016-07-28 70 views
2

我使用angularJs已完成,我用這個功能在我的控制器從數據庫我需要調用一個函數時,先前的功能

this.callServer = function callServer(criteria) { 
    ctrl.searchParameters = criteria; 
    ctrl.isLoading = true; 
    var start = $scope.itemsPerPage * ($scope.currentPage - 1); 
    var limit = $scope.itemsPerPage; 
    service.getRandomsItems(criteria, start, limit).then(
     function(result) { 
      var remainder = $scope.totalItems % $scope.itemsPerPage 
      if (remainder > 0) 
       $scope.numPages = parseInt($scope.totalItems/$scope.itemsPerPage) + 1; 
      else 
       $scope.numPages = parseInt($scope.totalItems/$scope.itemsPerPage); 

      ctrl.displayed = result.randomsItems; 
      $scope.totalItems = result.total; 
      ctrl.isLoading = false; 
     }); 
    }; 
} 

得到的數據和我在我的控制器也調用這個函數來處理分頁問題

$scope.pageChanged = function(currentPage) { 
    $scope.currentPage = currentPage; 
    ctrl.callServer($scope.criteria); 
} 

正如你所看到的,我的功能callServer回報ctrl.displayed這是顯示在當前頁面的行

現在我想用新的一頁工作,所以我叫ctrl.callServer獲得新的頁面,然後我叫ctrl.selectCurrentPage()就是這樣

$scope.pageChanged = function(currentPage) { 
    $scope.currentPage = currentPage; 
    ctrl.callServer($scope.criteria); 
    ctrl.selectCurrentPage() // I want this function to be called when ctrl.callServer($scope.criteria) is finished 
} 

其中

ctrl.selectCurrentPage = function() { 
    ctrl.selection.push(this.displayed[i].userId); 
    ctrl.selectionRow.push(this.displayed[i]); 
} 

用簡單的英語我想ctrl.selectCurrentPage被調用時ctrl.callServer是finsih,並獲得新的數據 但沒有發生。

回答

0

剛剛從callServer返回承諾pageChanged使用它。

第一步:

this.callServer = function callServer(criteria) { 
    ctrl.searchParameters = criteria; 
    ctrl.isLoading = true; 
    var start = $scope.itemsPerPage * ($scope.currentPage - 1); 
    var limit = $scope.itemsPerPage; 

    return service.getRandomsItems(criteria, start, limit).then(function(result) { 
     var remainder = $scope.totalItems % $scope.itemsPerPage 
     if (remainder > 0) 
      $scope.numPages = parseInt($scope.totalItems/$scope.itemsPerPage) + 1; 
     else 
      $scope.numPages = parseInt($scope.totalItems/$scope.itemsPerPage); 

     ctrl.displayed = result.randomsItems; 
     $scope.totalItems = result.total; 
     ctrl.isLoading = false; 
    }); 
}; 

然後:

$scope.pageChanged = function(currentPage) { 
    $scope.currentPage = currentPage; 
    ctrl.callServer($scope.criteria).then(function() { 
     ctrl.selectCurrentPage(); 
    }); 
} 

儘量避免回調地獄。請閱讀the article獲取一些有用的信息。

+0

它的作品非常感謝:)您非常喜歡..... –

+0

@BelalOthman酷:) –

0

將它作爲回調傳遞;

this.callServer = function callServer(criteria, callback) { 
          ctrl.searchParameters = criteria; 
          ctrl.isLoading = true; 
          var start = $scope.itemsPerPage * ($scope.currentPage - 1); 
          var limit = $scope.itemsPerPage; 
          service.getRandomsItems(criteria, start, limit).then(
              function(result) { 
               var remainder = $scope.totalItems % $scope.itemsPerPage 
               if (remainder > 0) 
                $scope.numPages = parseInt($scope.totalItems/$scope.itemsPerPage) + 1; 
               else 
                $scope.numPages = parseInt($scope.totalItems/$scope.itemsPerPage); 
               ctrl.displayed = result.randomsItems; 
               $scope.totalItems = result.total; 
               ctrl.isLoading = false; 
               callback(); 
              }); 
         }; 

        } ]); 



$scope.pageChanged = function(currentPage) { 

        $scope.currentPage = currentPage; 
        ctrl.callServer($scope.criteria, ctrl.selectCurrentPage); 
       } 
+0

感謝您的幫助:) –

+0

不客氣;) –

相關問題