Infinite Lists

由于手机不适合使用多页面显示posts,Infinite Lists成为各种新闻、咨询类app的标配。为了在ionic框架中使用到Infinite Lists,我们首先学习ion-list。

ion-list

ion-list使用

在ionic中,通过使用ion-list和ion-item来展示数据,通过ng-repeat循环输出,如demo(使用ionic start mydemo tabs命令生成ionic demo)中的templates/tab-friends.html中显示:

	<ion-list>
<ion-item ng-repeat="friend in friends" type="item-text-wrap" href="#/tab/friend/{{friend.id}}">
{{friend.name}}
</ion-item>
</ion-list>

假如我们返回到html的数据,每次都为20项,则但屏幕划到最后一项或前几项时,则需要监听检测到,并且载入下一个20项数据,从而达到无止境的下滑刷新载入更多的目标。这里,ionic提供了ion-infinite-scroll directive。

ion-infinite-scroll function is more like a scroll detector: it detects when a user is scrolled to a given point above the bottom of the view (default 1%, or 99% of the way down), and executes a function.

所以需要在html中添加这个directive,如下:

    <ion-list>
<ion-item ng-repeat="friend in friends" type="item-text-wrap" href="#/tab/friend/{{friend.id}}">
{{friend.name}}
</ion-item>
<ion-infinite-scroll on-infinite="addFriends()"></ion-infinite-scroll>
</ion-list>

在controller中添加addFriends()函数,和载入数据。

同时,在重新完全载入数据后,需要发送一个scroll.infiniteScrollComplete事件,告诉directive,我们完成了这个动作,系统会清理scroller和为下一次的载入数据,重新绑定这一事件。

修改friends controller

通过修改controller,让list一次载入20个friends数据,这样达到Infinite Lists效果,如下修改js/controllers.js中FriendsCtrl:

.controller('FriendsCtrl', function($scope, Friends) {
var currentStart = 0;
$scope.friends = []; $scope.addFriends = function() {
for (var i = currentStart; i < currentStart+20; i++) {
$scope.friends.push(Friends.get(currentStart));
} currentStart += 20;
$scope.$broadcast('scroll.infiniteScrollComplete');
}; $scope.addFriends();
//$scope.friends = Friends.all();
})

service添加mock数据

在service端,调用Friends factory如下:

.factory('Friends', function() {

// Might use a resource here that returns a JSON array

// Some fake testing data
var friends = [
{ id: 0, name: 'Scruff McGruff' },
{ id: 1, name: 'G.I. Joe' },
{ id: 2, name: 'Miss Frizzle' },
{ id: 3, name: 'Ash Ketchum' },
{ id: 4, name: 'Scruff McGruff' },
{ id: 5, name: 'G.I. Joe' },
{ id: 6, name: 'Miss Frizzle' },
{ id: 7, name: 'Ash Ketchum' },
{ id: 8, name: 'Scruff McGruff' },
{ id: 9, name: 'G.I. Joe' },
{ id: 10, name: 'Miss Frizzle' },
{ id: 11, name: 'Ash Ketchum' },{ id: 0, name: 'Scruff McGruff' },
{ id: 12, name: 'G.I. Joe' },
{ id: 13, name: 'Miss Frizzle' },
{ id: 14, name: 'Ash Ketchum' },
{ id: 15, name: 'Scruff McGruff' },
{ id: 16, name: 'G.I. Joe' },
{ id: 17, name: 'Miss Frizzle' },
{ id: 18, name: 'Ash Ketchum' },
{ id: 19, name: 'Scruff McGruff' },
{ id: 20, name: 'G.I. Joe' },
{ id: 21, name: 'Miss Frizzle' },
{ id: 22, name: 'Ash Ketchum' },
{ id: 23, name: 'Scruff McGruff' },
{ id: 24, name: 'G.I. Joe' },
{ id: 25, name: 'Miss Frizzle' },
{ id: 26, name: 'Ash Ketchum' },
{ id: 27, name: 'Scruff McGruff' },
{ id: 28, name: 'G.I. Joe' },
{ id: 29, name: 'Miss Frizzle' },
{ id: 30, name: 'Ash Ketchum' },
{ id: 31, name: 'Scruff McGruff' },
{ id: 32, name: 'G.I. Joe' },
{ id: 33, name: 'Miss Frizzle' },
{ id: 34, name: 'Ash Ketchum' },
{ id: 35, name: 'Scruff McGruff' },
{ id: 36, name: 'G.I. Joe' },
{ id: 37, name: 'Miss Frizzle' },
{ id: 38, name: 'Ash Ketchum' },
{ id: 39, name: 'Scruff McGruff' }
]; return {
all: function() {
return friends;
},
get: function(friendId) {
// Simple index lookup
return friends[friendId];
}
}

测试

使用python命令,测试:

python -m SimpleHTTPServer 8000

ion-infinite-scroll 参数

<ion-content ng-controller="MyController">
<ion-infinite-scroll
on-infinite="loadMore()"
distance="1%">
</ion-infinite-scroll>
</ion-content>

主要有三个参数:

  1. on-infinite:expression;含义:What to call when the scroller reaches the bottom.

  2. distance (optional) string; 含义:The distance from the bottom that the scroll must reach to trigger the on-infinite expression. Default: 1%.

  3. icon(optional) string;含义:The icon to show while loading. Default: 'ion-loading-d'.

也可以添加ng-if来判断是否还有更多数据可以载入,如:

<ion-infinite-scroll
ng-if="moreDataCanBeLoaded()"
icon="ion-loading-c"
on-infinite="loadMoreData()">
</ion-infinite-scroll>

更多的ion-list使用,参照官网

Exploring Ionic Lists的更多相关文章

  1. Ionic2系列——Ionic 2 Guide 官方文档中文版

    最近一直没更新博客,业余时间都在翻译Ionic2的文档.之前本来是想写一个入门,后来觉得干脆把官方文档翻译一下算了,因为官方文档就是最好的入门教程.后来越翻译越觉得这个事情确实比较费精力,不知道什么时 ...

  2. Ionic实战 自动升级APP(Android版)

    Ionic 框架介绍 Ionic是一个基于Angularjs.可以使用HTML5构建混合移动应用的用户界面框架,它自称为是"本地与HTML5的结合".该框架提供了很多基本的移动用户 ...

  3. Ionic 2.0.0-rc.1 发布,HTML5 移动应用框架

    Ionic 2.0.0-rc.1 发布了,Ionic Framework 是个高级的 HTML5 移动端应用框架,是个很漂亮的使用 HTML5 开发混合移动应用前端框架.本次更新内容如下: Bug 修 ...

  4. [转]Ionic – Mobile UI Framework for PhoneGap/Cordova Developers

    本文转自:http://devgirl.org/2014/01/20/ionic-mobile-ui-framework-for-phonegapcordova-developers/ Ionic i ...

  5. [转]Cordova + Ionic in Visual Studio - 101 Tutorial [Part I]

    本文转自:http://binarylies.ghost.io/cordova-ionic-in-visual-studio/ Hi everyone, I thought about lending ...

  6. Ionic 2 Guide

    Ionic 2 Guide 最近一直没更新博客,业余时间都在翻译Ionic2的文档.之前本来是想写一个入门,后来觉得干脆把官方文档翻译一下算了,因为官方文档就是最好的入门教程.后来越翻译越觉得这个事情 ...

  7. WebApp开发框架Ionic+AngularJS+Cordova

    目前的手机APP有三类:原生APP.WebAPP.HybridApp:HybridApp结合了前两类APP各自的优点,越来越流行. Ionic Ionic是一个新的.可以使用HTML5构建混合移动应用 ...

  8. Hybrid UI framework shootout: Ionic vs. Famo.us vs. F7 vs. OnsenUI

    1 Introduction In the past 2 years I’ve been working intensively on mobile applications, mostly hybr ...

  9. ionic学习教程地址梳理

    Ionic是一个新的.可以使用HTML5构建混合移动应用的用户界面框架,它自称为是"本地与HTML5的结合".该框架提供了很多基本的移动用户界面范例,例如像列表(lists).标签 ...

随机推荐

  1. dpkg 被中断,您必须手工运行 sudo dpkg -configure -a 解决

    E: dpkg 被中断,您必须手工运行 sudo dpkg --configure -a 解决此问题. E: dpkg 被中断,您必须手工运行 sudo dpkg --configure -a 解决此 ...

  2. 通过Greasemonkey实现网页图片自动点击

    昨天受一个朋友所托,实现了一个在特定网页自动点击某超链接图片实现网页跳转功能的JavaScript脚本. 工具就是Firefox的Greasemonkey扩展插件.代码如下: // ==UserScr ...

  3. Atitit .html5刮刮卡的gui实现总结

    Atitit .html5刮刮卡的gui实现总结 #----两个案例canvas或者wScratchPad-1.4.4 1 #----1.添加panel  ,这个十mask div.....posti ...

  4. paip.判断字符是否中文与以及判读是否是汉字uapi python java php

    paip.判断字符是否中文与以及判读是否是汉字uapi python java php   ##判断中文的原理 注意: 中文与汉字CJKV 的区别..日本,韩国,新加坡,古越南等国家也用汉字,但不是中 ...

  5. paip.常用汉字形声字大全3500字

    paip.常用汉字形声字大全3500字 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/at ...

  6. Redis 环境搭建与使用(C#)

    Redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis是一个key-value存储系统.和M ...

  7. 菜鸟日记-HTML-表格与表单

    一.表格 <table></table> width:宽度.可以用像素或百分比表示. border:边框,常用值0 cellpadding:内容跟单元格边框的边距.常用值0 a ...

  8. Leetcode 13 Roman to Integer 字符串处理+STL

    题意:将罗马数字1到3999转化成自然数字,这里用了STL库map将罗马字符映射到自然数字. I,V,X,L,C,D,M -> 1,5,10,50,100,500,1000 m[s[i]]< ...

  9. 15系统函数&数据类型转换(必学)-大话数据库视频教程

    大纲:系统函数的用法,case...when的用法,cast关键字的用法,convert的用法 优酷超清地址: 腾讯超清地址: 百度网盘下载地址:http://pan.baidu.com/s/1dDe ...

  10. SVN仓库删除最近的提交,还原到某日期以前的版本(svn仓库 删除最近几次更改)

    由于某日删除了SVN仓库的大量内容,现在突然想恢复,又要保留LOG的连贯性(恢复出来的已删除文件,会是新增,没有之前的历史Log了),所以才有了这需求. Dump版本库的版本(457以后版本不要) - ...