You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular报错:无法绑定到'ngForOf',求解决方案

解决Angular NG0303: Can't bind to 'ngForOf' 错误

针对你遇到的NG0303错误,即便根模块已经导入BrowserModule和CommonModule,仍可以从以下几个方向排查:

  • 特性模块需单独导入CommonModule
    如果你将CourseListComponent归类到某个特性模块(比如PlannedCoursesModule)中,必须给该特性模块单独导入CommonModule。BrowserModule仅作用于根模块,特性模块需要通过导入CommonModule来获取*ngFor这类通用结构指令的使用权。示例如下:

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { CourseListComponent } from './course-list/course-list.component';
    
    @NgModule({
      declarations: [CourseListComponent],
      imports: [CommonModule]
    })
    export class PlannedCoursesModule { }
    
  • 验证组件声明的准确性
    再次核对AppModule的配置:

    • 确认CourseListComponent的@Component装饰器配置正确,模板路径没有写错
    • 检查declarations数组中是否准确包含CourseListComponent,无拼写错误
  • 排查模板语法细节
    虽然该错误核心原因是模块配置,但也可以检查*ngFor的语法逻辑:

    <!-- 建议变量名保持单复数对应,提升可读性 -->
    <tr *ngFor="let course of courses">
      <td>{{ course.coursecode }}</td>
      <td>{{ course.coursename }}</td>
      <td>{{ course.description }}</td>
    </tr>
    
  • 清理项目缓存
    缓存异常也可能导致这类奇怪的错误,执行以下命令后重启项目:

    ng clean
    ng serve --open
    

内容的提问来源于stack exchange,提问作者Peter Sabio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 11:23:22