ngSubmit 未以 Angular 反应形式执行

Posted

技术标签:

【中文标题】ngSubmit 未以 Angular 反应形式执行【英文标题】:ngSubmit not executing in Angular Reactive Form 【发布时间】:2020-05-30 08:13:17 【问题描述】:

我是 Angular 的新手,并且在观看教程时正在搞乱反应形式。我的 ngSubmit() 函数没有执行,当我单击提交按钮时没有任何反应,花一个小时试图找到根本原因。有人可以帮忙吗?

员工.ts

export class Employee 

  private name: string;
  private email: string;
  private salary: number;
  private notes?: string;


App.component.html

  <div class="container-fluid">
  <app-header></app-header>
</div>

<div class="row justify-content-lg-center">
  <div class="col-md-4">
    <app-employee-list></app-employee-list>
  </div>

  <div class="col-md-4">
    <app-employee-form></app-employee-form>
  </div>
</div>

员工组件.html

<form [formGroup]="reactiveForm" (submit)="employeeSubmit(this.reactiveForm.value)">
  <div class="input-group mb-3">
    <div class="input-group-prepend">
      <span class="input-group-text" id="basic-addon1">@</span>
    </div>
    <input formControlName="name" type="text" class="form-control" placeholder="Username" aria-label="Username"
           aria-describedby="basic-addon1">
  </div>

  <div class="input-group mb-3">
    <input formControlName="email" type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username"
           aria-describedby="basic-addon2">
    <div class="input-group-append">
      <span class="input-group-text" id="basic-addon2">@example.com</span>
    </div>
  </div>


  <div class="input-group mb-3">
    <div class="input-group-prepend">
      <span class="input-group-text">$</span>
    </div>
    <input formControlName="salary" type="text" class="form-control" aria-label="Amount (to the nearest dollar)">
    <div class="input-group-append">
      <span class="input-group-text">.00</span>
    </div>
  </div>

  <div class="input-group">
    <div class="input-group-prepend">
      <span class="input-group-text">With textarea</span>
    </div>
    <textarea formControlName="notes" class="form-control" aria-label="With textarea"></textarea>
  </div>

  <div class="input-group">
  <button type="button" class="btn btn-primary">Submit</button>
  </div>

</form>

Employee-form-component.ts

import  Component, OnInit  from '@angular/core';
import FormBuilder, FormGroup from '@angular/forms';

@Component(
  selector: 'app-employee-form',
  templateUrl: './employee-form.component.html',
  styleUrls: ['./employee-form.component.css']
)
export class EmployeeFormComponent implements OnInit 

  reactiveForm: FormGroup;

  constructor(private formBuilder: FormBuilder) 
    this.reactiveForm = this.formBuilder.group(
      'name': [''],
      'email': [''],
      'salary': [''],
      'notes': ['']
    );
  

  ngOnInit() 
  

  employeeSubmit(value) 
    // tslint:disable-next-line:no-debugger
    debugger;
    console.log(this.reactiveForm.value);
  


【问题讨论】:

【参考方案1】:

您的按钮应输入submit,而不是输入button

<button type="submit">Submit</button>

将类型设置为button 是按钮不应提交表单的具体说明。当表单中有其他不是主要提交按钮的按钮时,这很有用。

【讨论】:

哈。发生在我们所有人身上:)

以上是关于ngSubmit 未以 Angular 反应形式执行的主要内容,如果未能解决你的问题,请参考以下文章

Angular 材料 2 在组件之间共享反应形式

Angular2(ngSubmit)不工作

Bootstrap Glyphicons 未以 angular2 显示

模型驱动形式:验证在 Angular 2 中无法按预期工作

Angular 2中的模板驱动形式和反应形式有啥区别

Angular 5-反应形式和动态形式之间的区别