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

Submitting Forms (ngSubmit)

表单的一般完整写法:
在这里插入图片描述

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

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

{
   
  "id":1,
  "name":"pikachu",
  "type":"fire"
}

修改 HTML, pokemon-template-form.component.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:

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

在这里插入图片描述

相关推荐

  1. Angular自定义异步验证

    2024-01-01 22:02:03       32 阅读
  2. Angular 14带来了类型化和独立组件

    2024-01-01 22:02:03       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-01 22:02:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-01 22:02:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-01 22:02:03       82 阅读
  4. Python语言-面向对象

    2024-01-01 22:02:03       91 阅读

热门阅读

  1. Linux:20个linux常用命令

    2024-01-01 22:02:03       60 阅读
  2. js获取某天日期

    2024-01-01 22:02:03       55 阅读
  3. 国内低代码平台介绍及优势盘点

    2024-01-01 22:02:03       48 阅读
  4. 微服务(8)

    2024-01-01 22:02:03       56 阅读
  5. Golang标准库sync的使用

    2024-01-01 22:02:03       54 阅读
  6. 源码解析:mybatis调用链之获取sqlSession

    2024-01-01 22:02:03       59 阅读
  7. 浅析 Dockerfile 构建缓存:原理与优化方法

    2024-01-01 22:02:03       56 阅读