Form group does not update angular template

Asked

Viewed 475 times

0

I am using formgroup and need to fill in the fields of my formgroup receiving data from an array.

I tried something like:

My Form Group:

produtoForm = this.fb.group({
    nomeproduto: [''],
    valorcompra: [null],
    valorvenda: [null],
    skuprincipal: [''],
    indicepersonalizado: [null],
    ean: [''],
    gtin: [''],
    marca: [''],
    estoque: [null],
    prazoproducao: [''],
    altura: [null],
    largura: [null],
    comprimento: [null],
    peso: [null],
    linkfoto1: [''],
    linkfoto2: [''],
    linkfoto3: [''],
    linkfoto4: [''],
    linkfoto5: [''],
    linkfoto6: [''],
    video: [''],
  });

I have a product variable that contains product information that I need to play on this formgroup. I tried something like:

this.produtoForm.controls = produto;

I also tried to:

this.produtoForm.controls.value = produto;

If I give you a console.log(this.productForm.Controls.value) my data is there, however my template is not updated with the data.

Example of my template:

<form [formGroup]="produtoForm" novalidate #f="ngForm">
  <div class="row">
    <div class="col-12 col-md-4">
      <div class="card-body">
        <div class="md-form form-lg input-modal">
          <input formControlName="nomeproduto" required type="text" name="nomeproduto" id="nomeproduto" class="form-control">
          <label id="labelnome" for="nomeproduto">Nome do Produto</label>
        </div>
      </div>
    </div>
  </div>

If I use with [(ngModel)] my template works as expected, but I get a Warning of which it is derogatory to use both together. How can I send these changes to my template?

@Edit:

I realized that updates the template if I use this way:

(<FormControl>this.produtoForm.controls['nomeproduto'])
.setValue(this.produto.nome);

Have some way to pass all values of my product variable to existing controllers?

2 answers

0

there is a function that assigns values of an object in the form provided that the object has the same attributes as the form: this.produtoForm.patchValue(produto);

0


patchValue

Mutile values

this.produtoForm.patchValue({nomeproduto: 'Test'}); 

setValue

unique value

this.produtoForm.nomeproduto.setValue('Test');

Browser other questions tagged

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