在页面加载时调用离子无限滚动函数

Jad*_*ani 17 scroll infinite angularjs ionic-framework ionic

我正在使用离子框架来创建一个基本的Feed应用程序.我得到了复习,但我的离子无限滚动有问题.

Feed.html

<ion-content ng-controller="FeedController" > 
    <ion-refresher pulling-text="Pull to refresh.." on-refresh="get_feeds()" refreshing-text="Fetching ..." refreshing-icon="ion-loading-b">
    </ion-refresher>
    <ion-list>
        <ion-item ng-repeat="question in questions">
            <div class="item item-divider">
{{question.question}}
            </div>
            <div class="item" ng-bind-html="question.answer | trustHtml">
            </div>
        </ion-item>
    </ion-list>
    <ion-infinite-scroll
        on-infinite="get_more()"
        distance="1%">
    </ion-infinite-scroll>
</ion-content>
Run Code Online (Sandbox Code Playgroud)

Feed.js

 (function(){
    "use strict";
    angular.module( 'app.controllers' ).controller( 'FeedController', 
        [ 'eTobb', 'Users', '$scope', '$timeout', function( eTobb, Users, $scope, $timeout) {

            var pageIndex = 1;

            $scope.questions = [];

            $scope.get_feeds = function(pageIndex){
                eTobb.get($scope.apiCall, { user_id: Users.id, page: 0 }, function(response) {
                    if ( response ){
                        $scope.questions = $scope.questions.concat(response);
                        console.log($scope.questions);
                    }
                })
                .finally(function(){
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                });
            };

            $scope.get_more = function(){
                $scope.get_feeds(pageIndex);
                pageIndex = pageIndex + 1; 
                console.log('why the hell am i being called?');
            };
        }
        ]);
})(); 
Run Code Online (Sandbox Code Playgroud)

问题

在on-infinite属性上定义的方法在页面加载时被调用两次,然后在每次到达页面底部时被正常调用(一次).有谁知道为什么会这样?

RMD*_*RMD 52

我也遇到了这个问题,但是使用ng-if没有解决问题,我也不相信它是解决这个问题的正确方法.

根据Ionic的文档,您只能使用ng-if表示没有其他数据可用,并且阻止无限卷轴对"get_more"方法进行额外调用.

我发现在页面加载期间发生这种意外的"get_more()"调用的原因是由于无限滚动器认为在第一次加载期间没有足够的内容(即:初始加载没有提供足够的结果来填充页面),因此它立即启动另一个加载请求.

在我的特定场景中,我已经返回了足够多的记录来填充页面,但无限卷轴仍在请求更多.我的列表主要是用图像填充的,我认为由于加载图像和检查页面是否填满之间的时间问题,滚动条不恰当地调用了"get_more()".

在任何情况下,我的问题的解决方案是告诉它不对负载执行立即边界检查; 你使用immediate-check属性执行此操作,即:

<ion-infinite-scroll immediate-check="false" on-infinite="vm.localDataManager.getData()" ng-if="vm.localDataManager.canGetData()" distance="1%">
Run Code Online (Sandbox Code Playgroud)

  • 很棒的解释!我知道问题是这样的,但我无法找到解决方案,而且你的解决方案比使用ng-if:D更有意义 (2认同)

L42*_*L42 5

RMD的答案在计算机上为我解决了这个问题,但是当我在Android上运行我的应用程序时,vm.loadMore()即使immediate-check="false"在我的ion-infinite-scroll元素上设置了我的功能也立即被调用.我通过向vm.initialSearchCompleted = false;控制器添加一个辅助变量来解决它,并对我的ion-infinite-scroll元素添加如下:

<ion-infinite-scroll
  on-infinite="vm.loadMore()"
  distance="1%"
  immediate-check="false"
  ng-if="vm.canLoadMoreResults && vm.initialSearchCompleted">
</ion-infinite-scroll>
Run Code Online (Sandbox Code Playgroud)

当我对我的vm.search()功能的初始调用完成后,我设置了vm.initialSearchCompleted = true;.这解决了这个问题.