[Angular] 笔记 16:模板驱动表单 - 选择框与选项

油管视频: Select & Option (Template Driven Forms)

Select & Option

pokemon.ts 中新增 interface:

ts 复制代码
export interface Pokemon {
  id: number;
  name: string;
  type: string;
  isCool: boolean;
  isStylish: boolean;
  acceptTerms: boolean;
}

// new interface
export interface PokemonType {
  key: number;
  value: string;
}

修改 pokemon-template-form.component.ts:

ts 复制代码
import { Component, OnInit } from '@angular/core';
import { Pokemon, PokemonType } from '../models/pokemon';
import { PokemonService } from '../services/pokemon.service';

@Component({
  selector: 'app-pokemon-template-form',
  templateUrl: './pokemon-template-form.component.html',
  styleUrls: ['./pokemon-template-form.component.css'],
})
export class PokemonTemplateFormComponent implements OnInit {
  pokemon!: Pokemon;

  // create dropdown for Pokemon type
  pokemonType: PokemonType[] = [
    {
      key: 0,
      value: 'Fire',
    },
    {
      key: 1,
      value: 'Water',
    },
  ];

  constructor(private pokemonService: PokemonService) {}

  toggleIsCool(object: any) {
    console.log(object);
    this.pokemon.isCool = !this.pokemon.isCool;
  }

  ngOnInit() {
    this.pokemonService.getPokemon(1).subscribe((data: Pokemon) => {
      this.pokemon = data;
    });
  }
}

在 HTML 中显示,pokemon-template-form.component.html:

html 复制代码
<form #form="ngForm">
  Pokemon Name:
  <input type="text" [(ngModel)]="pokemon.name" name="name" />
  <label>
    <input
      type="radio"
      name="isCool"
      [value]="true"
      #pokemonName="ngModel"
      [ngModel]="pokemon.isCool"
    />Pokemon is cool?
  </label>
  <label>
    <input
      type="radio"
      name="isCool"
      [value]="false"
      [ngModel]="pokemon.isCool"
      (ngModelChange)="toggleIsCool($event)"
    />Pokemon is NOT cool?
  </label>
  <label>
    <input
      type="checkbox"
      name="acceptTerms"
      [(ngModel)]="pokemon.acceptTerms"
    />
    Accept Terms?
  </label>
  <label>
    Pokemon Type:
    <select name="pokemonType" [ngModel]="pokemon?.name">
      <option
        *ngFor="let type of pokemonType"
        [value]="type.key"
        [selected]="type.value === pokemon.type"
      >
        {{ type.value }}
      </option>
    </select>
  </label>
</form>
<div>
  MODEL: {{ pokemon | json }} FORM: {{ form.value | json }} ERROR:
  <div *ngIf="!pokemonName.pristine">NOT PRINSTINE ANYMORE IT IS DIRTY!</div>
</div>

选择框与下拉列表正常工作:

如果要发送值给后端,那么将 HTML 中的 [value]="type.key" 改为 [value]="type.value"

相关推荐
code_YuJun7 分钟前
corepack 作用
前端
千寻girling8 分钟前
Koa.js 教程 | 一份不可多得的 Node.js 的 Web 框架 Koa.js 教程
前端·后端·面试
全栈前端老曹9 分钟前
【MongoDB】Node.js 集成 —— Mongoose ORM、Schema 设计、Model 操作
前端·javascript·数据库·mongodb·node.js·nosql·全栈
code_YuJun10 分钟前
pnpm-workspace.yaml
前端
三水不滴10 分钟前
计网ping原理
经验分享·笔记·计算机网络
天才熊猫君12 分钟前
“破案”笔记:iframe动态加载内容后,打印功能为何失灵?
前端
prog_610326 分钟前
【笔记】思路分享:各种大模型免费当agent后台
笔记·大语言模型·agent·cursor
五月君_29 分钟前
炸裂!Claude Opus 4.6 与 GPT-5.3 同日发布:前端人的“自动驾驶“时刻到了?
前端·gpt
Mr Xu_34 分钟前
前端开发中CSS代码的优化与复用:从公共样式提取到CSS变量的最佳实践
前端·css
凯尔萨厮42 分钟前
Maven(Windows下载安装)
笔记·maven