当前位置: 首页 > news >正文

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

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

Select & Option

在这里插入图片描述
pokemon.ts 中新增 interface:

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:

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 typepokemonType: 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:

<form #form="ngForm">Pokemon Name:<input type="text" [(ngModel)]="pokemon.name" name="name" /><label><inputtype="radio"name="isCool"[value]="true"#pokemonName="ngModel"[ngModel]="pokemon.isCool"/>Pokemon is cool?</label><label><inputtype="radio"name="isCool"[value]="false"[ngModel]="pokemon.isCool"(ngModelChange)="toggleIsCool($event)"/>Pokemon is NOT cool?</label><label><inputtype="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"

在这里插入图片描述

http://www.lryc.cn/news/269582.html

相关文章:

  • Webpack基础使用
  • 扭蛋机小程序搭建:打造互联网“流量池”
  • 解决VNC连接Ubuntu服务器打开终端出现闪退情况
  • flutter是什么
  • GET和POST请求
  • 基于电商场景的高并发RocketMQ实战-Broker写入读取流程性能优化总结、Broker基于Pull模式的主从复制原理
  • 前端DApp开发利器,Ant Design Web3 正式发布 1.0
  • [RoarCTF 2019]Easy Java(java web)
  • Abaqus许可管理策略
  • 对采集到的温湿度数据,使用python进行数据清洗,并使用预测模型进行预测未来一段时间的温湿度数据。
  • 嵌入式SOC之通用图像处理之OSD文字信息叠加的相关实践记录
  • Java日期工具类LocalDateTime
  • 从C到C++1
  • [Angular] 笔记 18:Angular Router
  • 微服务全链路灰度方案介绍
  • 低代码开发OA系统 低代码平台如何搭建OA办公系统
  • 构建Python的Windows整合包教程
  • 《整机柜服务器通用规范》由OCTC正式发布!浪潮信息牵头编制
  • Linux:修改和删除已有变量
  • 【23.12.29期--Spring篇】Spring的 IOC 介绍
  • 【Python排序算法系列】—— 选择排序
  • 会议室占用时间段 - 华为OD统一考试
  • 计算机网络复习5
  • React Hooks 面试题 | 05.精选React Hooks面试题
  • 2024收入最高的编程语言
  • Android笔记(二十三):Paging3分页加载库结合Compose的实现分层数据源访问
  • Python实现马赛克图片处理
  • 你能描述下你对vue生命周期的理解?在created和mounted这两个生命周期中请求数据有什么区别呢?
  • 【经典算法】有趣的算法之---蚁群算法梳理
  • 第八届视觉、图像与信号处理国际会议(ICVISP 2024) | Ei, Scopus双检索