skip to Main Content

I have created a form using Angular and my save button isn’t working. The back end is working per Postman and SQL Server. I’m using Visual Studio Code and dotnet run on the back end and ng serve on the front end.

When I click the save button, nothing happens at all.

This is my Pessoas.Component.ts file

import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { Pessoa } from 'src/app/Pessoa';
import { PessoasService } from 'src/app/pessoas.service';

@Component({
  selector: 'app-pessoas-component',
  templateUrl: './pessoas-component.component.html',
  styleUrls: ['./pessoas-component.component.css']
})
export class PessoasComponentComponent implements OnInit {

  formulario : any;
  tituloFormulario : string = 'Nova Pessoa | By Raf Hassegawa';

  constructor(private pessoasService: PessoasService) {}
  ngOnInit(): void {
    
    this.formulario = new FormGroup({
      nome : new FormControl(null),
      sobrenome : new FormControl(null),
      idade : new FormControl(null),
      profissao : new FormControl(null)
    });
  }

  EnviarFormulario(): void {
    const pessoa : Pessoa = this.formulario.value;
    this.pessoasService.SalvarPessoa(pessoa).subscribe(resultado => {
      alert('Pessoa inserida com sucesso!');
    });

  }

}

This is my HTML file (which calls the EnviarFormulario method + the save button):

<form class="formulario" (ngSubmit)="EnviarFormulario()"> 

    <div class="container p-5">
        <div class="row">
            <div class="col-6 border border-light rounded p-5 shadow" *ngIf="formulario">
                <h6>{{tituloFormulario}}</h6>
                <hr/>
    
                <form [formGroup]="formulario">
                    <div class="form-group">
                        <label>Nome</label>
                        <input type="text" class="form-control form-control-sm" formControlName="nome">
                    </div>
                </form>
    
                <form [formGroup]="formulario">
                    <div class="form-group">
                        <label>Sobrenome</label>
                        <input type="text" class="form-control form-control-sm" formControlName="sobrenome">
                    </div>
                </form>
    
                <form [formGroup]="formulario">
                    <div class="form-group">
                        <label>Idade</label>
                        <input type="number" class="form-control form-control-sm" formControlName="idade">
                    </div>
                </form>
    
                <form [formGroup]="formulario">
                    <div class="form-group">
                        <label>Profissão</label>
                        <input type="text" class="form-control form-control-sm" formControlName="profissao">
                    </div>
                </form>
    
                <div class="container">    
                    <div class="row">
                            <button type="submit">
                                Salvar
                            </button>
                            <button type="button">
                                Voltar
                            </button>
                    </div>
                </div>
    
            </div>
        </div>
    </div>

</form>

2

Answers


  1. Chosen as BEST ANSWER

    I changed the html code and it worked! Thanks guys! This is what I got:

    <form [formGroup]="formulario" (ngSubmit)="EnviarFormulario()"> 
        <div class="container p-5">
          <div class="row">
            <div class="col-6 border border-light rounded p-5 shadow" *ngIf="formulario">
              <h6>{{tituloFormulario}}</h6>
              <hr/>
              <div class="form-group">
                <label>Nome</label>
                <input type="text" class="form-control form-control-sm" formControlName="nome">
              </div>
              <div class="form-group">
                <label>Sobrenome</label>
                <input type="text" class="form-control form-control-sm" formControlName="sobrenome">
              </div>
              <div class="form-group">
                <label>Idade</label>
                <input type="number" class="form-control form-control-sm" formControlName="idade">
              </div>
              <div class="form-group">
                <label>Profissão</label>
                <input type="text" class="form-control form-control-sm" formControlName="profissao">
              </div>
              <div class="container">    
                <div class="row">
                  <button type="submit">
                    Salvar
                  </button>
                  <button type="button">
                    Voltar
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </form>
      
    

  2. Your submit button is inside in the form but why you put many formgroup in your form ?
    Try to remove it.

    <form [formGroup]="formulario" class="formulario" (ngSubmit)="EnviarFormulario()"> 
    
        <div class="container p-5">
            <div class="row">
                <div class="col-6 border border-light rounded p-5 shadow" *ngIf="formulario">
                    <h6>{{tituloFormulario}}</h6>
                    <hr/>
        
                    
                        <div class="form-group">
                            <label>Nome</label>
                            <input type="text" class="form-control form-control-sm" formControlName="nome">
                        </div>
                   
        …
    <button type="submit">
       Salvar
    </button>
    ….
    </form>
                   
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search