[英]Differing *ngFor behaviour between Angular 8 and Angular 10
I have recently updated a project from Angular 8 to Angular 10 and have had a few issues.我最近将一个项目从 Angular 8 更新到 Angular 10 并且遇到了一些问题。 I am looping over two separate lists and displaying data
我正在遍历两个单独的列表并显示数据
import { Component, Input } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div *ngFor="let column of columns; let i of index">
Field: {{column['field']}}
<br>
Name: {{column['name']}}
<br>
Index: {{i}}
</div>`
})
export class AppComponent {
@Input() name: string;
columns: object[] = [
{field: 'FIELD_A', name:'NAME_A'},
{field: 'FIELD_B', name:'NAME_B'}
];
index: number[] = [0,1];
}
In angular 8 my output looks like在 angular 8 我的 output 看起来像
Field: FIELD_A
Name: NAME_A
Index: [object Object]
Field: FIELD_B
Name: NAME_B
Index: [object Object]
In angular 10 my output looks like在 angular 10 我的 output 看起来像
Field:
Name:
Index: 0
Field:
Name:
Index: 1
My questions are:我的问题是:
Note that substituting let i of index
with let i = index
gives the correct consistent output for both请注意,将
let i of index
替换为let i = index
可以为两者提供正确一致的 output
Field: FIELD_A
Name: NAME_A
Index: 0
Field: FIELD_B
Name: NAME_B
Index: 1
You should not use your variant of NgForOf-syntax because it does not correspond to API reference ( https://angular.io/api/common/NgForOf ).您不应该使用您的 NgForOf-syntax 变体,因为它不对应于 API 参考( https://angular.io/api/common/NgForOf )。 You should use something like:
你应该使用类似的东西:
<div *ngFor="let column of columns; index as i">
Field: {{column['field']}}
<br>
Name: {{column['name']}}
<br>
Index: {{i}}
</div>
About the Index you are using the wrong syntax, here is the right one:关于您使用错误语法的索引,这是正确的:
<div *ngFor="let column of columns; let index = index">
About the version migration: Angular 10 uses a strict mode on template type checking by default.关于版本迁移: Angular 10 默认对模板类型检查使用严格模式。 As it can't check it with your syntax, i would assume it doesn't display it.
由于它无法用您的语法检查它,我会假设它不会显示它。 You can change it by calling directly the property:
您可以通过直接调用属性来更改它:
Field: {{column.field}}
<br>
Name: {{column.name}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.