ianhuang筆記THE ADVENTURER'S JOURNAL
知識世界・持續探索中

ADVENTURE JOURNAL · 探索筆記

Angular Reactive Forms:模型、驗證與提交

任務導覽 TABLE OF CONTENTS

整理兩種表單的適用情境,補上型別化 FormGroup 與完整範例。

適用:原筆記的Angular CLI/NgModule專案;新範例以Angular 20的standalone元件說明。建立專案前按官方版本表選Node與TypeScript,不能把舊專案直接套最新CLI。

比較

方式資料模型適合
Template-driven由ngModel在模板建立簡單欄位、少量規則
ReactiveTypeScript明確建立control多欄位、動態規則、較多測試

新版另有Signal Forms,需依實際Angular版本評估,不把本篇Reactive API混寫成Signal API。FormControl管理單欄、FormGroup管理鍵名集合、FormArray管理動態清單。

先依 Angular CLI 篇 建立相容版本的 standalone 專案。下例可存為 src/app/app.component.ts;src/main.ts 改為從 ./app/app.component 匯入 AppComponent,並使用 bootstrapApplication(AppComponent, appConfig),保留 CLI 產生的 appConfig。Angular 20 的預設 root 可能叫 App、位於 app.ts,檔名與啟動類別要一併對齊,不能只貼程式卻仍啟動舊元件。

完整元件

import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
  selector: 'app-root', standalone: true, imports: [ReactiveFormsModule],
  template: `<form [formGroup]="form" (ngSubmit)="submit()">
    <label>名稱 <input formControlName="name"></label>
    <label>Email <input formControlName="email" type="email"></label>
    <button [disabled]="form.invalid">送出</button>
  </form><p>{{ result }}</p>`
})
export class AppComponent {
  form = new FormGroup({
    name: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
    email: new FormControl('', { nonNullable: true, validators: [Validators.required, Validators.email] })
  });
  result = '';
  submit(): void {
    this.form.markAllAsTouched();
    if (this.form.invalid) return;
    this.result = JSON.stringify(this.form.getRawValue());
  }
}

空表單停用送出,填Ian與ian@example.test後顯示JSON。nonNullable使reset回初始字串而不是null;getRawValue包含disabled欄位,value可能不含,所以選擇需符合目的。

常見問題

setValue要求完整結構,patchValue允許部分欄位。valueChanges訂閱要清理,可用takeUntilDestroyed;大量訂閱或HTTP搜尋要處理去抖與取消。不要在同一輸入同時使用ngModel與formControlName。前端email驗證只檢查格式,不證明信箱存在或歸屬。

參考資料

冒險者留言

comments powered by Disqus

搜尋筆記

輸入關鍵字,探索所有筆記。

ESC 關閉 · 搜尋標題、標籤與文章全文