我试了所有我能猜到的语法都不能让它工作!

<!--- 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>

唯一对我有效的方法是

  1. 在控制器类中创建方法

    识别(索引、帖子:帖子){

<ion-card *ngFor="#post of posts;trackBy:identify">
</ion-card>

这是唯一的办法吗?我不能只为trackBy指定内联属性名称吗?

推荐答案

正如@Eric comments 中所指出的,在大量阅读和玩耍之后,下面是如何在angular2中使用trackBy

  1. 首先你需要知道它和angular1的语法不同,现在你需要用;把它和for循环分开.

Usage 1: Track by property of object

 // starting v2. 1 this will throw error, you can only use functions in trackBy from now on

<ion-card *ngFor="let post of posts;trackBy:post?.id">
</ion-card> // **DEPRECATED**
---or---
<ion-card *ngFor="let post of posts;trackBy:trackByFn">
</ion-card>

给你,请安格拉尔

  1. 创建一个局部变量post;
  2. 告诉trackBy等待,直到这个局部变量准备好"通过使用Elvis运算符 变量名‘,则使用其id作为跟踪器.

所以

// starting v2. 1 this will throw error, you can only use functions in trackBy from now on

*ngFor="#post of posts;trackBy:post?.id"

与Angular 的1相同的是什么

ng-repeat="post in posts track by post.id"

Usage 2: Track using your own Function

@Page({
    template: `
        <ul>
            <li *ngFor="#post of posts;trackBy:identify">
              {{post.data}}
            </li>
        </ul>
    `
})
export class HomeworkAddStudentsPage {
    posts:Array<{id:number,data:string}>;   

    constructor() {
        this.posts = [  {id:1,data:'post with id 1'},
                        {id:2,data:'post with id 2'} ];
    }

    identify(index,item){
      //do what ever logic you need to come up with the unique identifier of your item in loop, I will just return the object id.
      return post.id 
     }

}

trackBy可以使用callback的名称,它将为我们调用它,并提供两个参数:循环的索引和当前项.

为了在Angular 1中实现同样的效果,我曾经:

<li ng-repeat="post in posts track by identify($index,post)"></li>

app.controller(function($scope){
  $scope.identify = function(index, item) {return item.id};
});

Angular相关问答推荐

在forkjoin中使用NGXS状态数据

如何在自定义验证器中获取所有表单控件(字段组动态创建)

接收错误NullInjectorError:R3InjectorError(_AppModule)[config—config]....>过度安装Angular—Markdown—Editor

从Angular 14更新到15个多边形.ts问题

在Angular 为17的独立零部件中使用@NGX-平移

在Angular中使用ngFor进行表乘法

如何在 Angular 中每 x 秒从 REST api 加载数据并更新 UI 而不会闪烁?

具有两个订阅方法的 Angular 16 takeUntilDestroyed 运算符

如何有条件地更新 Angular 路由解析器数据?

使用 BehaviorSubject 从服务更新 Angular 组件

Observable .do() 运算符 (rxjs) 的用例

清除Angular2中的输入文本字段

如何以Angular 2 获取当前路由自定义数据?

如何将一个组件放入Angular2中的另一个组件中?

angular2中的httpinterceptor类似功能是什么?

如何使用 Angular 6 工作的 ngModel 创建自定义输入组件?

如何使用formControlName和处理嵌套的formGroup?

@viewChild 不工作 - 无法读取未定义的属性 nativeElement

如何将 @Input 与使用 ComponentFactoryResolver 创建的组件一起使用?

ngIf - 判断后表达式已更改