Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxFormArray setValue #524

Open
VoteForPedro opened this issue Sep 29, 2021 · 4 comments
Open

RxFormArray setValue #524

VoteForPedro opened this issue Sep 29, 2021 · 4 comments

Comments

@VoteForPedro
Copy link

VoteForPedro commented Sep 29, 2021

Is there a way to replace existing formarray controls?

I have RxFormArray that is empty, When im receiving data, im trying to set it to form array:
`
var eqs = (this.form.get("equipments") as RxFormArray);

let items = result.equipments.map((a) => this.formBuilder.formGroup(new EquipmentViewModel(a)));

eqs.setValue(items);
`
setValue function call throws error:
There are no form controls registered with this array yet. If you're using ngModel,
you may want to check next tick (e.g. use setTimeout).

@rxweb/reactive-form-validators: "2.1.4"

@ajayojha
Copy link
Member

@VoteForPedro How you are creating the nested FormGroup? please share the little bit code snippet.

@VoteForPedro
Copy link
Author

VoteForPedro commented Sep 30, 2021

export class EquipmentViewModel {
  @unique()
  id: string;

  @required()
  equipmentName: string;

  constructor(summary?: OrderEquipmentSummary) {
    this.id = summary?.id;
    this.equipmentName = summary?.equipmentName;
  }
}

export class OrderViewModel {
  @propArray(EquipmentViewModel)
  equipments: EquipmentViewModel[];

  constructor(detail?: OrderDetail) {
    this.equipments = detail?.equipments.map((eq) => {
      return new EquipmentViewModel(eq);
    }) ?? [];
  }
}

...

let viewModel = new OrderViewModel();
this.form = this.formBuilder.formGroup(viewModel) as RxFormGroup;

@ajayojha
Copy link
Member

ajayojha commented Oct 1, 2021

@VoteForPedro uses the push method of 'RxFormArray'.

here is the example:

var eqs = (this.form.get("equipments") as RxFormArray);
result.equipments.forEach((a) => eqs.push(this.formBuilder.formGroup(new EquipmentViewModel(a))));

@VoteForPedro
Copy link
Author

@ajayojha yes, im already using push. But how can i replace an array of existing controls?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants