[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 可以看到表单被提交:

相关推荐
૮・ﻌ・6 分钟前
Vue3:组合式API、Vue3.3新特性、Pinia
前端·javascript·vue3
前端不太难6 分钟前
RN + TypeScript 项目越写越乱?如何规范架构?
前端·javascript·typescript
神算大模型APi--天枢6467 分钟前
全栈自主可控:国产算力平台重塑大模型后端开发与部署生态
大数据·前端·人工智能·架构·硬件架构
苏打水com7 分钟前
第十五篇:Day43-45 前端性能优化进阶——从“可用”到“极致”(对标职场“高并发场景优化”需求)
前端·css·vue·html·js
@大迁世界15 分钟前
08.CSS if() 函数
前端·css
无名-CODING15 分钟前
栈与队列学习笔记
java·笔记
NZT-4818 分钟前
C++基础笔记(二)队列deque,queue和堆priority_queue
java·c++·笔记
Moment21 分钟前
小米不仅造车,还造模型?309B参数全开源,深度思考完胜DeepSeek 🐒🐒🐒
前端·人工智能·后端
苏打水com25 分钟前
第十六篇:Day46-48 前端安全进阶——从“漏洞防范”到“安全体系”(对标职场“攻防实战”需求)
前端·javascript·css·vue.js·html
5C2427 分钟前
从思想到实践:前端工程化体系与 Webpack 构建架构深度解析
前端·前端工程化