[Angular] 笔记 4:ngFor

ngFor 是一个 for 循环,只能用于循环遍历 list,不能用于遍历单个实体。

下图中的 pokemons 通常是数据库中的数据:
在这里插入图片描述
例子:
app.components.ts:

// 使用类型检查
interface Pokemon {
   
  id: number;
  name: string;
  type: string;
  // isCool: boolean;
}

@Component({
   
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
   
  pokemons: Pokemon[] = [
    {
   
      id: 1,
      name: 'pikachu',
      type: 'electric',
    },
    {
   
      id: 2,
      name: 'squirtle',
      type: 'water',
    },
    {
   
      id: 3,
      name: 'charmander',
      type: 'fire',
    },
  ];

  constructor() {
   }
}

app.component.html:

<table>
  <thead>
    <th>Index</th>
    <th>Name</th>
    <th>Type</th>
  </thead>
  <tbody>
    <tr *ngFor="let pokemon of pokemons; let i = index">
      <td>{
  { i }}</td>
      <td>{
  { pokemon.name }}</td>
      <td>{
  { pokemon.type }}</td>
    </tr>
  </tbody>
</table>

Web 页面:

在这里插入图片描述

相关推荐

  1. Angular 2 学习笔记

    2023-12-22 06:34:04       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-22 06:34:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-22 06:34:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-22 06:34:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-22 06:34:04       20 阅读

热门阅读

  1. uni-app获取音频播放时长

    2023-12-22 06:34:04       39 阅读
  2. HTTPS

    HTTPS

    2023-12-22 06:34:04      42 阅读
  3. 解决Electron中WebView加载部分HTTPS页面白屏的方法

    2023-12-22 06:34:04       36 阅读
  4. Python PDF格式转PPT格式

    2023-12-22 06:34:04       40 阅读
  5. Spring Boot Web中文文档

    2023-12-22 06:34:04       37 阅读
  6. 【SpringBoot实战】基于阿里云实现文件上传

    2023-12-22 06:34:04       39 阅读
  7. vue获取图片的blob传给django后端

    2023-12-22 06:34:04       42 阅读