How to add dynamically FormControl to FormGroup

前端 未结 1 1965
谎友^
谎友^ 2021-01-22 01:26

I have this plunker. I am creating form components dynamically, based on model (defined in app.ts), and cannot add

formControlName = \"n

相关标签:
1条回答
  • 2021-01-22 01:37

    To keep form value in sync with your custom model i would subscribe to control.valueChanges

    let control = new FormControl(this.model.data);
    control.valueChanges.subscribe(x => {
      this.model.data = x;
    });
    this.form.addControl(this.model.name, control);
    

    to keep in sync form model and view i would bind FormControl to reactive directive i.e. formControl

    datepicker.component.html

    <input [formControl]="form.get(model.name)">
    

    Modified Plunker

    0 讨论(0)
提交回复
热议问题