我需要在上一个函数完成时调用一个函数

时间:2016-07-28 14:36:11

标签: javascript angularjs

我正在使用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]);
}

in Simple English我想在ctrl.callServer为finsih时调用ctrl.selectCurrentPage并获取新数据 但那不会发生。

2 个答案:

答案 0 :(得分: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);
                }

答案 1 :(得分: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以获取一些有用的信息。