[英]NgSubmit not working (button is already inside the form)
當我提交我的表單時,我創建的 function onSubmit() 沒有被調用。 我已經搜索過了,但其他答案主要是按鈕在表單之外,而不是我的情況,也許我錯過了一些東西。
HTML:
<form ng-submit="onSubmit()" [formGroup]="messageForm">
<fieldset>
<label for="name">Nome e Sobrenome</label>
<input type="text" name="name" id="name" required formControlName="name">
<label for="email">Email</label>
<input type="email" name="email" id="email" required>
<label for="tel">Telefone</label>
<input type="text" name="tel" id="tel" required>
</fieldset>
<fieldset>
<label for="message">Mensagem</label>
<textarea name="message" id="message" cols="30" rows="10" required></textarea>
<button type="submit">Enviar</button>
</fieldset>
</form>
組件.TS
export class ContatoComponent{
title="Entre em contato conosco";
messageForm = new FormGroup({
name: new FormControl(""),
email: new FormControl(""),
tel: new FormControl(""),
message: new FormControl("")
})
constructor() { }
onSubmit(){
console.log("oi")
}
}
我發現了丟失的東西。 在 app.module.ts 我沒有導入 ReactiveFormsModule,如果有人遇到同樣的問題:這是答案:
在 app.module.ts 中:
import { ReactiveFormsModule } from '@angular/forms';
imports:[
...
ReactiveFormsModule
]
而不是 ng-submit try (ngSubmit),我猜 ng-submit 是針對舊版本的。 它對我有用。 只是改變
(ng-submit)="onSubmit()"
至
(ngSubmit)="onSubmit()"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.