$ http GET两次无限滚动

时间:2014-12-08 14:12:32

标签: javascript angularjs

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是我的指令,如果我删除其中的所有内容并只留下警报('f'),它只会触发一次。也许我在其他地方做错了什么?

2 个答案:

答案 0 :(得分:3)

尝试通过使用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;

        });

    }
};

答案 1 :(得分:0)

给我immediate-check="false"离子无限滚动解决了我的问题。


像这样

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