kianoush dortaj
kianoush dortaj

Reputation: 451

not set ouput value in reactive form - angular9

I have a form and in this for I use the from array :

private InitialFrom(): void {
  this.addElectricMoneyFG = this.fromBuilder.group({
    locales: this.fromBuilder.array([])
  })
}

and i have a component , that have output by this value :

    <multi-language-data-form
      [selectLanguage]="selectLanguage"
      [fields]="fields"
      sticy="true"
      (formValue)="setValue($event)"
    ></multi-language-data-form>

and it return this value from output :

locales:
     0:
      languageId: 1
      moreAccountInfo: "gh"
      name: "ghgh"
    1:
      languageId: 2
      moreAccountInfo: "gh"
      name: "ghgh"

and when i want to update the form from that value it not set value from the locals :

setValue(value): void {
   this.addElectricMoneyFG.patchValue({ ...value });
 }

whats the problem ? how can i set the value from the form ???

Upvotes: 0

Views: 41

Answers (1)

Meadow
Meadow

Reputation: 317

You do not need to create a formBuilder inside the main formBuilder, you can just do this:

  private InitialFrom(): void {
    this.addElectricMoneyFG = this.formBuilder.group({
      locales: []
    })
  }

To patch the value to the form, you can pass LanguageId from (formValue) event, then get the value with find, and patch desired value to the FormGroup.

To use patchValue method, you need to precise the form field as key of the object parameter you pass to patchValue(). for example here it's "locales"

  setValue(languageId: number): void {
    var getLocale = this.fields.find(x => x.languageId == languageId);
    this.addElectricMoneyFG.patchValue({ locales: getLocale });
  }

Upvotes: 1

Related Questions