开发过程当中须要对展现内容实现上拉加载的更多效果,原本觉得实现没有什么难度,ionic自己就提供了ion-infinite-scroll组件可以完成咱们的开发须要。
先上代码ios
<ion-view view-title="{{i18n.org_member_info_label}}"> <ion-content> <div ng-repeat="item in table.trs" on-hold="table.touch(item)" class="item item-input-inset" ng-class-odd="'oddRow'" ng-click="showDetail(item)"> <p ng-bind="item.realName" class="input-label w25p"></p> <p ng-bind="item.workType"></p> <i class="icon ion-ios-arrow-right icon-pos-right"></i> </div> <ion-infinite-scroll on-infinite="table.query()" distance="2%" ng-if="table.isLoadMore" immediate-check="false"></ion-infinite-scroll> </ion-content> </ion-view>
$scope.table = { ths: CONFIG.project.orgTeamHead, trs: [], isLoadMore: true, currentPage: 0, maxPage: 1, /** * @ngdoc method * @name orgInfoCtrl#table.query * @methodOf table.query * @return {undefined} * @description Get members in this organization by workerContractList api. * */ query() { this.currentPage += 1; if(this.currentPage > this.maxPage){ this.isLoadMore = false; return; } apiService.getWorkerContractList({ sid: $scope.userInfo.sid, team_id: data.id, request_status: $scope.i18n.request_status_complete_label, flag: 1, page: this.currentPage, limit: $scope.limit }); }, /** * @ngdoc method * @name orgInfoCtrl#table.dealData * @methodOf table.dealData * @param {Object} data - Server response. * @return {Undefined} * @description Handle response from server. * */ dealData(data) { this.maxPage = Math.ceil(data.count / $scope.limit); $scope.table.trs = _.concat($scope.table.trs, data); this.isLoadMore = result.length >= $scope.limit; $timeout(() => { $scope.$broadcast("scroll.infiniteScrollComplete"); }); } }; $scope.$on('stateChangeSuccess', function() { $scope.table.query() });
这里我截取了部分项目中的代码来讲明实现上拉加载更多的实现,官方文档已经写得很清楚了,这里主要是提一下如何解决屡次调用的问题。
一、immediate-check属性判断是否在页面载入后当即检查滚动边界 默认为true(这将致使页面载入后当即调用刷新),若是不设置为false时,可能咱们在进入页面后直接调用了屡次的加载更多loadMore函数(这里是query函数),设置FALSE后须要咱们在进入页面后在stateChangeSuccess中调用加载更多的loadMore函数。
二、这里在处理了数据dealData中,使用了一个定时器。若是不使用这个定时器,虽然数据请求回来了,可是内容尚未充满整个屏幕,这时已经广播$broadcast加载动做已经结束,它就会再自动执行一次或者屡次加载,形成数据错误
三、若是咱们全部的数据都请求完成,记得设置ng-if=false,控制不在执行上拉加载更多事件
四、ng-repeat可能在手机上调试时,一样出现一次下拉,结果屡次调用,这时候用collcection-repeat代替,ng-repeat在渲染页面的时候会触发页面的滚动, 致使上拉事件的触发api