假设您有* ngFor,遍历对象数组。如果您添加trackby函数并返回常量,如果您可能将arrayOfObjects替换为arrayOfObjects2,会产生什么问题呢?如果trackby返回null,index,1
,我也会看到相同的行为// test.component.ts
public arrayOfObjects = [{a: 1, b: 2}, {a: 3, b: 4}]
public arrayOfObjects2 = [{a: 5, b: 6}, {a: 7, b: 8}]
ngOnInit() {
setTimeout(() => this.arrayOfObjects = this.arrayOfObjects2, 2000);
}
iReturnConstant(index, item) {return 1;}
// test.component.html
<tr *ngFor="let entry of arrayOfObjects; trackBy: iReturnConstant">
<th>{{ entry.a }}</th>
<td>{{ entry.b }}</td>
</tr>