NgSubmit not working (button is already inside the form)

Issue

When i submit my form the function onSubmit() that i created is not being call.
I already searched, but the other answers wasmostly that the button was outside the form, not my case, maybe i’m missing something.

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>

COMPONENT.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")
    }

 }

Solution

I’ve discoreved what was missing! Inside the app.module.ts i was not importing ReactiveFormsModule.
If someone is having the same issue, here is the answer:

inside app.module.ts:

    import { ReactiveFormsModule } from '@angular/forms';

    imports:[
      ...
      ReactiveFormsModule
    ]

Answered By – Fernanda_kipper

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published