angularjs - 页面加载时调用 Ionic 无限滚动函数

标签 angularjs scroll infinite ionic-framework

我正在使用 ionic 框架创建一个基本的 Feed 应用程序。我进行了复习,但我的 ionic 无限滚动遇到了问题。

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>

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?');
            };
        }
        ]);
})(); 

问题

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

最佳答案

我也遇到了这个问题,但是使用 ng-if 并没有解决问题,我也不认为这是解决这个问题的正确方法。

根据 Ionic 的 documentation ,您只需使用 ng-if 来指示没有更多数据可用,并防止无限滚动器对“get_more”方法进行额外调用。

我发现页面加载期间出现意外的“get_more()”调用的原因是无限滚动条认为第一次加载期间没有足够的内容(即:初始加载没有提供足够的结果)填充页面),因此它立即发起另一个加载请求。

在我的特定场景中,我返回了足够多的记录来填充页面,但是无限滚动器仍然请求更多。我的列表大部分都充满了图像,我认为滚动条不恰本地调用了“get_more()”,因为加载图像和检查页面是否已填充之间的时间问题。

无论如何,我的问题的解决方案是告诉它不要在加载时立即执行边界检查;您可以使用立即检查属性来执行此操作,即:

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

关于angularjs - 页面加载时调用 Ionic 无限滚动函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27938475/

相关文章:

css - Android 版 Chrome 上的纯 CSS 视差滚动效果

java - 使用 Jackson 将对象转换为 JSON 时的无限递归

AngularJS 和谷歌搜索引擎优化索引

javascript - 使用 then 的 Promise 链接

jquery - 使用选择框滚动到页面上的某个位置

javascript - jquery : animate list scrollLeft/scrollRight on mouse wheel

list - 解释为什么 append/3 在这些情况下会产生无限数量的解决方案

swift - 创建没有 framedrops 的无尽 cgpath

javascript - 如何从 Angular 服务中的 API 获取数据并将其存储以供将来任何人使用

javascript - 如何通过 ng-click 将 $scope 传递给 Controller ​​?