[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"

相关推荐
wdfk_prog19 分钟前
[Linux]学习笔记系列 -- [drivers][I2C]I2C
linux·笔记·学习
0思必得031 分钟前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
Misnice34 分钟前
Webpack、Vite、Rsbuild区别
前端·webpack·node.js
青茶36035 分钟前
php怎么实现订单接口状态轮询(二)
前端·php·接口
觉醒大王1 小时前
哪些文章会被我拒稿?
论文阅读·笔记·深度学习·考研·自然语言处理·html·学习方法
大橙子额1 小时前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js
方安乐1 小时前
科普:股票 vs 债券的区别
笔记
爱喝白开水a3 小时前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
董世昌413 小时前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
傻小胖3 小时前
22.ETH-智能合约-北大肖臻老师客堂笔记
笔记·区块链·智能合约