2016-07-28 82 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

不客气;) –

相关问题