Angular 17 新的流程控制

一、简介

本文主要介绍 Angular 17 中的模板语句中的条件语句 @if/@else-if/@else@for - of 以及 @switch 三种不同的流程控制。

整体语法是 @关键字 配合 {}, 语法与原生 JS 语法极为相似。

二、条件渲染 @if-@else

2.1)关键字

  • @if
  • @else
  • @else-if

2.2)示例

html 复制代码
<div>
  <button (click)="toggle()">toggle</button>
  @if(cond) {
      <div>This is A</div>
  } @else {
      <div>This is else</div>
  }
</div>
ts 复制代码
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-about',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './about.component.html',
  styleUrl: './about.component.css'
})
export class AboutComponent {
  cond = false;
  toggle() {
    this.cond = !this.cond;
  }
}

本组件中使用 toggle 函数完成模板不同的状态切换。

2.3)之前使用 *ngIf

html 复制代码
<div *ngIf="condition; else elseBlock">
  Content to render when the condition is true.
</div>
<ng-template #elseBlock>
   Content to render when the condition is false.
</ng-template>

我们看到 angular 17 中的流程控制 @if - @else 语句不再使用指令的方式, 而采用与常规的 js 语法很像的模板语法。

三、列表渲染

3.1)关键字

  • @for - of
  • track
  • @empty
  • $index
  • $even
  • $odd
  • $count
  • $first
  • $last

3.2)示例

html 复制代码
<div>
  @for (item of list; track item.id; let idx = $index, e = $even, o = $odd, c = $count,  f = $first, l = $last) {
  #{{ item.id }} - {{item.name}} - {{idx}} - {{e}} - {{o}} - {{c}} - {{f}} - {{l}}
  } @empty {
  <div>-_-</div>
  }
</div>
ts 复制代码
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-list',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './list.component.html',
  styleUrl: './list.component.css'
})
export class ListComponent {
  list = [
    {id: 0, name: 'Alice'},
    {id: 1, name: 'Bob'},
  ]
}

3.3)之前使用 *ngFor

html 复制代码
<ul>
  <li *ngFor="let item of list">
	{{item.id}} {{item.name}}
  </li>
</ul>

@for-of 语法遍历数组更加贴近 JS 原生语法,使用模板化,代替指令化。同时提供了 $开头的常用变量来方便我们的开发。使用 track 字段表示数组中的值与视图关联。类似于 React 中的 key。

四、@switch

4.1)关键字

  • @switch
  • @case
  • @default

4.2)示例

html 复制代码
<button (click)="handleState()">handleState</button>

@switch (state) {
  @case (1) {
    <p>Viewing content of first page</p>
  }
  @case (2) {
    <p>Viewing content of second page</p>
  }
  @default {
    <p>No page selected</p>
  }
}
ts 复制代码
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-switch',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './switch.component.html',
  styleUrl: './switch.component.css'
})
export class SwitchComponent {
  state = 1

  handleState() {
    this.state++
  }
}

使用 handleState 控制 state 调节swtich 模板需要转变的状态。

4.3)之前使用 *ngSwitch

html 复制代码
<div [ngSwitch]="page">
      <p *ngSwitchCase="1">Viewing content of first page</p>
      <p *ngSwitchCase="2">Viewing content of second page</p>
      <p *ngSwitchCase="3">Viewing content of third page</p>
      <p *ngSwitchDefault>No page selected</p>
</div>

五、小结

在Anglar中原本使用指令的方式进行条件渲染,列表渲染,还有 switch 渲染,全部支持了模板新语法。新的语法与 JS 源本身更加的贴近,本身更加容易理解,也不需要多学习,看一个遍就会了。

相关推荐
IT_陈寒26 分钟前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
codingWhat1 小时前
介绍一个手势识别库——AlloyFinger
前端·javascript·vue.js
代码老中医1 小时前
2026年CSS彻底疯了:这6个新特性让我删掉了三分之一JS代码
前端
不会敲代码11 小时前
Zustand:轻量级状态管理,从入门到实践
前端·typescript
踩着两条虫1 小时前
VTJ.PRO 双向代码转换原理揭秘
前端·vue.js·人工智能
扉川川1 小时前
OpenClaw 架构解析:一个生产级 AI Agent 是如何设计的
前端·人工智能
远山枫谷1 小时前
一文理清页面/组件通信与 Store 全局状态管理
前端·微信小程序
codingWhat1 小时前
手撸一个「能打」的 React Table 组件
前端·javascript·react.js
HelloReader1 小时前
Tauri 应用安全从开发到发布的威胁防御指南
前端
bluceli1 小时前
WebAssembly实战指南:将高性能计算带入浏览器
前端·webassembly