Maintain input value of a reactive form after an Submit

Asked

Viewed 58 times

0

I have a simple reactive form that, when saved, erases the log from my input. I’m trying to maintain value after saving it. But I’m not getting it.

HTML

<form [formGroup]="form" (ngSubmit)="submit()" class="form-inline">
  <div class="row">
    <div class="form-group col-lg-6 col-md-12 col-sm-12">
      <label class="control-label">Currículo Lattes</label>
      <input type="text" class="form-control"
        id="curriculoLattes"
        placeholder="Ex.: Link do currículo"
        formControlName="linkCurriculoLattes"
        value="text">

      <button
        type="submit"
        class="btn btn-success"
        style="margin: 10px 0;"
        disableButtonDuringRequest
        [disabled]="!form.hasChanges">
          <i class="fa fa-check"></i>
          Salvar Currículo Lattes
      </button>
</form>

TS

salvar() {
  const model = this.form.getRawValue();

  let request;
  if (this.edicao) {
    request = this.pessoaService
      .alterarCurriculoLattes(this.idPessoa, model);
  } else {
    request = this.pessoaService
      .salvarCurriculoLattes(this.idPessoa, model);
  }

  this.subscribtions.add(
    request.subscribe((result) => {
      if (result) {
        this.edicao = true;
        this.setFormularioSemAlteracao(this.form);
        this.store.dispatch(actions.salvarCurriculoLattes(model));

        this.services
          .notification
          .exibirMensagemDeSucesso('Currículo lattes salvo com sucesso.');
      }
    })
  );
}

setFormularioSemAlteracao(form = null) {
  if (form) {
    form.markAsPristine();
    form.markAsUntouched();
    form.hasChanges = false;
   } else {
     this.form.markAsPristine();
     this.form.markAsUntouched();
     this.form.hasChanges = false;
   }
}

I tried to insert

value="linkCurriculoLattes ? linkCurriculoLattes : null"

But I was unsuccessful. also tried

[value]="linkCurriculoLattes ? linkCurriculoLattes : null"

but returned the error:

Identifier 'linkCurriculoLattes' is not defined. The Component declaration, template variable declarations, and element not contain such a Member

  • What that method setFormularioSemAlteracao does? Put your content in question

  • @Ready costamilam.

  • At some point you call form.reset() or form.patchValue() or form.setValue()? These are the methods generally used to change the values of a control

  • That’s the question, I don’t call at any time haha.

  • You can go stamping your code and see when the form is reset. Not ideal, but you can pre-save the form value and set it again at the end of the submission

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.