CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
Angular 核心/课时/指令
RawBlame
课时目录12 节

Angular 核心

  1. 01Angular 是什么
  2. 02CLI 与项目结构
  3. 03组件与模板
  4. 04指令
  5. 05服务与依赖注入
  6. 06HttpClient
  7. 07Router
  8. 08表单
  9. 09RxJS 入门
  10. 10Signals 新特性
  11. 11Angular vs React/Vue
  12. 12综合练习

指令

*ngIf、*ngFor、属性绑定。

30 分钟

指令

指令 扩展 HTML 行为:结构型(*ngIf、*ngFor)改 DOM 树,属性型([class]、[style])改元素属性。Angular 17+ 另提供 @if / @for 控制流,语法更贴近 Vue 3 核心 模板。

学习目标

  • 使用 @if / @for 与经典 *ngDirectives
  • 掌握属性绑定、ngClass、ngStyle
  • 理解 Standalone 组件的 imports 声明

核心内容

控制流(推荐)

@Component({
  selector: "app-lesson-list",
  standalone: true,
  template: `
    @if (loading) {
      <p>加载中…</p>
    } @else if (error) {
      <p class="error">{{ error }}</p>
    } @else {
      <ul>
        @for (lesson of lessons; track lesson.slug) {
          <li [class.done]="lesson.completed">{{ lesson.title }}</li>
        } @empty {
          <li>暂无课时</li>
        }
      </ul>
    }
  `,
})
export class LessonListComponent {
  loading = false;
  error: string | null = null;
  lessons = [{ slug: "ng-intro", title: "Angular 是什么", completed: true }];
}

经典指令(存量代码常见)

<ul>
  <li *ngFor="let t of tracks; trackBy: trackBySlug">{{ t.title }}</li>
</ul>
<div *ngIf="user; else guest">欢迎,{{ user.name }}</div>
<ng-template #guest><p>请登录</p></ng-template>

使用 *ngFor / *ngIf 时,Standalone 组件需在 imports 加入 CommonModule。

属性绑定

<button [disabled]="!formValid" [class.active]="isActive">提交</button>
<img [attr.alt]="trackTitle" [style.width.px]="120" />
AngularVueReact
@if / *ngIfv-if{cond && <…>}
@for / *ngForv-for.map()
[class.x]:classclassName

企业项目常混用新旧语法;读代码时两种都要认。类型标注配合 TypeScript 核心。

练习

  • 用 @for 渲染 track 列表,加 @empty 占位
  • 用 [ngClass] 切换「已完成 / 进行中」样式

小结

指令 = 结构 + 属性扩展;优先学 @if/@for。 下一课服务与依赖注入。

加载本节练习…
← 上一课组件与模板下一课 →服务与依赖注入