在Angular中,循环遍历数组可以使用*ngFor指令。下面是一个示例代码:
在组件中定义一个数组:
export class AppComponent {
fruits: string[] = ['apple', 'banana', 'orange'];
}
在模板中使用*ngFor指令来循环遍历数组:
- {{ fruit }}
这段代码会生成一个无序列表,其中每个水果都会被显示为一个列表项。
输出结果:
- apple
- banana
- orange
你也可以通过使用索引变量来访问数组的索引:
- {{ i + 1 }}. {{ fruit }}
输出结果:
- 1. apple
- 2. banana
- 3. orange
下一篇:Angular: 需要属性绑定