How to append new FormGroup or FormControl to form

前端 未结 4 1175
灰色年华
灰色年华 2021-01-31 15:28

I have the following form in Angular created with FormBuilder:

constructor(private fb: FormBuilder) {
    this.myForm = fb.group({
             


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 16:08

    To add upon what @ranakrunal9 said.

    If you would like to use validators with addControl do the following:

    this.myForm.addControl('newControl', new FormControl('', Validators.required));
    

    Just don't forget to add the following import

    import {FormControl} from "@angular/forms";
    

    Reference to addControl: https://angular.io/api/forms/FormGroup#addControl

    Reference to FormControl: https://angular.io/api/forms/FormControl

提交回复
热议问题