How to iterate through formbuilder.group controls?

  Kiến thức lập trình

TypeScript

formGroup = this.fb.group({});

constructor(private readonly formBuilder: UntypedFormBuilder){}

ngOnInit() { this.setControls(); }


settControls() {
 this.formGroup.addControl('name', this.fb.control('', Validators.required));
 this.formGroup.addControl('age', this.fb.control('', Validators.required));
 this.formGroup.addControl('alias', this.fb.control('', Validators.required));
}

HTML

<ng-container *ngFor="let control of formGroup.controls">
  //show some formcontrol or component
</ng-container>

I have tried ngFor but that doesn’t work because form builder group is an object not an array. I know there is a formarray but that is not the way to go.

How do I iterate though the form group controls?

LEAVE A COMMENT