无限滚动,$http GET触发两次

infinite scrolling with $http GET firing twice

本文关键字:两次 滚动 http GET 无限      更新时间:2023-09-26
currentPage = 1;
$scope.loadPages = function () {
    Array.prototype.pushArray = function () {
        this.push.apply(this, this.concat.apply([], arguments));
    };
    if (currentPage < totalPages) {
        $scope.infiniteScoll = true;
        $http({
            url: "http://someurl.php",
            method: "GET",
            params: {
                "topicId": $stateParams.topicId,
                "pageNumber": currentPage
            }
        }).then(function (response) {
            $scope.posts.pushArray(response.data);
            alert('w2');
            currentPage = parseInt(response.data[0].currentPage) + 1;
            totalPages = parseInt(response.data[0].totalPages) + 1;

        }).
        finally(function () {
            $scope.infiniteScoll = false;
        });
    }
}

我会调试四个小时,仍然不知道为什么它会触发两次。 loadPages 是我的指令,如果我删除其中的所有内容并只留下 alert('f'),它只会触发一次。也许我在其他地方做错了什么?

尝试使用 infiniteScroll 布尔值阻止第二次调用,如以下代码所示

currentPage = 1;
$scope.loadPages = function () {
    //should be declared out of this function
    Array.prototype.pushArray = function () {
        this.push.apply(this, this.concat.apply([], arguments));
    };
    if(!$scope.infiniteScoll && currentPage < totalPages) {
        $scope.infiniteScoll = true;
        $http({
            url: "http://someurl.php",
            method: "GET",
            params: {
                "topicId": $stateParams.topicId,
                "pageNumber": currentPage
            }
        }).then(function (response) {
            $scope.posts.pushArray(response.data);
            alert('w2');
            currentPage = parseInt(response.data[0].currentPage) + 1;
            totalPages = parseInt(response.data[0].totalPages) + 1;

        }).
        finally(function () {
            $scope.infiniteScoll = false;
        });
    }
};

在离子无限卷轴上给出immediate-check="false"为我解决了这个问题。


喜欢这个

 <ion-infinite-scroll
            immediate-check="false"
            on-infinite="loadMore()"
            ng-if="hasMoreData"
            distance="1%">
 </ion-infinite-scroll>