【发布时间】:2016-07-19 06:47:16
【问题描述】:
尝试了所有我能猜到的语法都不能让它工作!
<!--- 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>
唯一对我有用的方法是
-
在控制器类中创建方法
识别(索引,发布:发布){ 返回 post.id }
和
<ion-card *ngFor="#post of posts;trackBy:identify">
</ion-card>
这是唯一的方法吗?我不能只为 trackBy 指定一个属性名称吗?
【问题讨论】:
-
你能告诉我 ngFor 中 trackBy 的用途吗?埃里克的回答似乎很好。
-
@micronyks 它允许你将一个函数传递给
*ngFor,NgFor转发给https://angular.io/docs/ts/latest/api/core/IterableDifferFactory-interface.htmlgithub.com/angular/angular/blob/master/modules/angular2/src/…(虽然还不知道差异是如何工作的) -
https://angular.io/docs/ts/latest/api/core/IterableDifferFactory-interface.html 未找到。还有其他链接吗? -
我仍然不清楚
trackBy的干净用法。 -
如果有人在
*ngFor中发布使用trackBy的示例,我将非常有帮助
标签: angular ngfor angularjs-track-by