Set field value Select dynamically in Angular reactive form

Asked

Viewed 344 times

0

I have the following select field:

.html

<div class="col-md-4">
     <div class="mda-form-group">
          <select class="mda-form-control" formControlName="state"
           (change)="getCities($event.target.value)"aria-placeholder="Select a State...">
               <option value="">--Selecione um valor--</option>
               <option *ngFor="let state of valuesState" [value]="state.stateId">{{state.name}}</option>
          </select>
          <label>Estado</label>
      </div>
</div>

every time a state is selected the city select is updated. The problem is that I put a function to search for Zip and update the fields automatically.

.Component.

  zipQuery() {
    let zipCode = this.valForm.get('address.zipCode').value
    //Nova variável "cep" somente com dígitos.
    var zip = zipCode.replace(/\D/g, '');
    if (zip != "") {
      var validateZip = /^[0-9]{8}$/;
      //Valida o formato do CEP.
      if (validateZip.test(zip)) {
        this.ZipCodeService.getZipCode(zip).subscribe(res => {
          this.data = res;
          this.resetAddressForm();
          this.populateDataForm(this.data);
        })
      }
    }
  }

  resetAddressForm() {
    this.valForm.patchValue({
      address: {
        addressName: null,
        number: null,
        complement: null,
        neighborhood: null,
        state: null
      }
    })
  }

  populateDataForm(data) {
    this.stateService.getStateUf(data.uf).subscribe((states: States[]) => {
      this.valuesState = states;
      console.log(this.valuesState);
    });
    this.valForm.patchValue({
      address: {
        addressName: data.logradouro,
        neighborhood: data.bairro,
        zipCode: data.cep,
        state: this.valuesState
      }
    });    

I’m getting the following error:

:4200/vendor.js:68903 ERROR Error: Error trying to diff 'Minas Gerais'. Only arrays and iterables are allowed
    at DefaultIterableDiffer.diff (:4200/vendor.js:89095)
    at NgForOf.ngDoCheck (:4200/vendor.js:30297)
    at callHook (:4200/vendor.js:67357)
    at callHooks (:4200/vendor.js:67317)
    at executeCheckHooks (:4200/vendor.js:67239)
    at selectIndexInternal (:4200/vendor.js:72036)
    at ɵɵadvance (:4200/vendor.js:72004)
    at CreateCustomersComponent_Template (ng:///CreateCustomersComponent.js:363)
    at executeTemplate (:4200/vendor.js:74125)
    at refreshView (:4200/vendor.js:73972)

And besides, even if I can solve the problem and popular this information, how do I leave the selected value?

Grateful,

  • 1

    it seems to me that valuesState is being used as the value of select and also as the options it is right to have an array of states

  • Thank you Eduardo! It had this problem as well, but the biggest problem was in an asynchronous call, which was returning the value after the command line.

No answers

Browser other questions tagged

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