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 源本身更加的贴近,本身更加容易理解,也不需要多学习,看一个遍就会了。

相关推荐
用户3423232376317几秒前
SPI 通信与高速外设驱动详解
后端
Csvn19 分钟前
CSS :has() 选择器实战:没有它之前我们写了多少冗余 JS
前端·css
梨子同志22 分钟前
TypeScript
前端
星栈25 分钟前
LiveView 表单真香,但 changeset 也真会坑人:实时校验、错误展示、前后端校验合一
前端·前端框架·elixir
魏祖潇26 分钟前
SDD 完整指南——Spec 端打底、Story 端交付、留白区
人工智能·后端
Slice_cy28 分钟前
JavaScript(ES6)
前端
用户2986985301436 分钟前
在 React 中使用 JavaScript 合并 Excel 文件
前端·javascript·react.js
橘子星41 分钟前
JavaScript this 指向全解实战指南
前端·javascript
何出无名之师41 分钟前
AIDL的一次调用链路追踪之二,如何和驱动打交道
前端