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

相关推荐
喜欢你,还有大家3 分钟前
FTP文件传输服务
linux·运维·服务器·前端
该用户已不存在7 分钟前
你没有听说过的7个Windows开发必备工具
前端·windows·后端
凯尔萨厮15 分钟前
Java学习笔记三(封装)
java·笔记·学习
Bi18 分钟前
Dokploy安装和部署项目流程
运维·前端
普通网友20 分钟前
前端安全攻防:XSS, CSRF 等防范与检测
前端·安全·xss
携欢22 分钟前
PortSwigger靶场之Reflected XSS into attribute with angle brackets HTML-encoded通关秘籍
前端·xss
小爱同学_26 分钟前
React知识:useState和useRef的使用
前端·react.js
再学一点就睡33 分钟前
双 Token 认证机制:从原理到实践的完整实现
前端·javascript·后端
wallflower202036 分钟前
滑动窗口算法在前端开发中的探索与应用
前端·算法
蚂蚁绊大象36 分钟前
flutter第二话题-布局约束
前端