尝试了我能猜出的每一种语法都无法使其有效!
<!--- THIS WORKS FINE --->
<ion-card *ngFor="#post of posts">
{{post|json}}
</ion-card>
<!--- BLANK PAGE --->
<ion-card *ngFor="#post of posts track by post.id">
{{post|json}}
</ion-card>
<!--- Exception : Cannot read property 'id' of undefined --->
<ion-card *ngFor="#post of posts;trackBy:post.id">
{{post|json}}
</ion-card>
<!--- Exception : Cannot read property 'undefined' of undefined --->
<ion-card *ngFor="#post of posts;trackBy:posts[index].id">
{{post|json}}
</ion-card>
<!--- Blank page no exception raised ! --->
<ion-card *ngFor="#post of posts;#index index;trackBy:posts[index].id">
{{post|json}}
</ion-card>
对我有用的唯一方法是
- 在控制器类中创建方法
识别(索引,帖子:帖子)
和
<ion-card *ngFor="#post of posts;trackBy:identify">
</ion-card>
这是唯一的方法吗?我不能只为trackBy指定内联的属性名称吗?
4 回答
正如您已经认识到的那样,使用函数是在Angular 2中使用
trackBy
的唯一方法官方文件说明https://angular.io/docs/ts/latest/api/common/index/NgFor-directive.html
关于
<ion-card *ngFor="let post of posts;trackBy:post?.id"></ion-card>
的所有其他信息都是错误的 . 从Angular 2.4.1开始,这也会在应用程序中引发错误 .正如在@Eric评论中指出的,经过大量的阅读和游戏,这里是如何在angular2中使用trackBy
;
分开 .Usage 1: Track by property of object
在这里你问angular2到
创建一个局部变量帖子;
你告诉trackBy要等到这个局部变量准备就绪“你这样做是通过使用elvis operator 'the question mark after the variable name',然后使用它的id作为跟踪器 .
所以
与棱镜1相同
Usage 2: Track using your own Function
trackBy可以采用回调的名称,它将为我们调用它提供2个参数:循环的索引和当前项 .
为了实现与Angular 1相同,我曾经做过:
trackBy背后的概念:
ngFor
of angular通过跟踪对象标识自动优化修改/创建/删除对象的显示 . 因此,如果您在列表中创建所有新对象,然后使用ngFor
,它将呈现整个列表 .让我们考虑一个场景,尽管所有
ngFor
优化,渲染仍然需要时间 . 在那种情况下,我们使用trackBy
. 因此,我们可以提供另一个参数来跟踪对象,而不是作为默认跟踪条件的对象标识 .一个运行的例子:
这个简单的解决方案适合我的场景
EDIT: 正如Jeremy Thille所建议的那样,你应该使用
let
而不是#
,因为在最新版本的Angular2中不推荐使用#
.