[Angular] 笔记 17:提交表单 - ngSubmit

Submitting Forms (ngSubmit)

表单的一般完整写法:

如果表单验证失败,必须 disable 提交按钮,阻止用户提交不合法的数据。

提交表单后,与表单对应的 json 数据 post 到后端:

json 复制代码
{
  "id":1,
  "name":"pikachu",
  "type":"fire"
}

修改 HTML, pokemon-template-form.component.html:

html 复制代码
<!-- 当类型为 submit 的按钮被点击后,此 handleSubmit 将运行 -->
<form #form="ngForm" (ngSubmit)="handleSubmit(form)">
  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.value"
        [selected]="type.value === pokemon.type"
      >
        {{ type.value }}
      </option>
    </select>
  </label>
  <!-- 增加提交表单按钮 -->
  <button type="submit" [disabled]="!form.valid">Save</button>
</form>
<div>
  MODEL: {{ pokemon | json }} FORM: {{ form.value | json }} ERROR:
  <div *ngIf="!pokemonName.pristine">NOT PRINSTINE ANYMORE IT IS DIRTY!</div>
</div>

实现 handleSubmit,pokemon-template-form.component.html:

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;
    });
  }
  
  handleSubmit(object: any) {
    console.log(object)
  }
}

运行 ng serve,点击 save button, 从 console 可以看到表单被提交:

相关推荐
星恒随风5 分钟前
Linux进程间通信(一):从匿名管道、命名管道到进程池,深入理解Pipe、FIFO与文件描述符
linux·运维·笔记·学习·状态模式
мо仙堡杠把子ご灬13 分钟前
Flex 自适应布局:告别 calc,用一个 height-auto 搞定
前端·javascript
霖霖71419 分钟前
【ESP32笔记】读取DHT11温湿度数据
笔记·单片机·mcu·物联网
Xudde.20 分钟前
DC-6 靶机渗透测试(Vulnhub Writeup)
笔记·学习·安全·web安全
xian_wwq22 分钟前
【学习笔记-一文读懂系列】一文读懂Jev模型
笔记·学习
环境栈笔记33 分钟前
指纹浏览器检查代理失败怎么排查?先分清代理、网络和检测服务
前端·人工智能·后端·自动化
ShineWinsu38 分钟前
对于HTML:HTML常见标签的解析—下
前端·css·面试·html·标签·表单·emmet
恋猫de小郭39 分钟前
Kotlin 2.5 的 Companion 改造,对 KMP 的重要升级
android·前端·flutter
Awh-41 分钟前
ARM 裸机开发 DAY8 学习笔记
arm开发·笔记·学习
带金箍的至尊宝1 小时前
系统架构设计师笔记 08:UML 用例图、类图、时序图
笔记·系统架构·uml