有条件地将 formGroup 添加到另一个 formGroup angular2

Bri*_*ley 6 angular2-forms angular

我正在尝试将一个 formGroup 添加到我的整体表单中,条件是在我的情况下特定字段属于某种类型。我添加此 formGroup 的函数如下所示:

  initSocial() {
      const group = <FormGroup>this.cardForm;
        group.addControl(
          'social': this._fb.group({
            'fb': new FormControl(this.currentCard.social.fb),
            'yt': new FormControl(this.currentCard.social.yt),
            'ig': new FormControl(this.currentCard.social.ig),
            'tw': new FormControl(this.currentCard.social.tw),
          })
        )
  }
Run Code Online (Sandbox Code Playgroud)

现在,在“社交”说“,”之后,冒号下出现打字稿错误。我无法对此进行测试以查看 addControl() 是否会起作用。

My function to fire initSocial is in ngOnInit and looks like this:

      if(this.currentCard.cardType === "brand"){
        this.initSocial();
      }
Run Code Online (Sandbox Code Playgroud)

Any help/tips/suggestions would be much appreciated.

And*_*riy 5

addControl() 功能看起来像:

/**
 * Add a control to this group.
 * @param {?} name
 * @param {?} control
 * @return {?}
 */
FormGroup.prototype.addControl = function (name, control) {
    this.registerControl(name, control);
    this.updateValueAndValidity();
    this._onCollectionChange();
};
Run Code Online (Sandbox Code Playgroud)

并需要 2 个参数:namecontrol,用逗号分隔。因此,只需尝试用逗号 ( , )替换'social' 之后的分号 ( : ):

group.addControl('social', this._fb.group({
  fb: this.currentCard.social.fb,
  yt: this.currentCard.social.yt,
  ig: this.currentCard.social.ig,
  tw: this.currentCard.social.tw,
}))
Run Code Online (Sandbox Code Playgroud)

此外,您不需要new FormControl(DEFAULT_VALUE),表单生成器已经做到了