NgSubmit not working (button is already inside the form)

1.4k Views Asked by At

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

 }
2

There are 2 best solutions below

0
Fernanda_kipper On BEST ANSWER

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
    ]
0
pranav annam On

Instead of ng-submit try (ngSubmit), ng-submit is for the older versions I guess. It worked for me. Just Change

(ng-submit)="onSubmit()"

to

(ngSubmit)="onSubmit()"