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

conditionally add formGroup to another formGroup angular2

我正在尝试将 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),
          })
        )
  }

现在我在 'social' 说“,”后冒号下出现打字稿错误。我无法对此进行测试以查看 addControl() 是否有效。

我启动 initSocial 的函数在 ngOnInit 中,如下所示:

      if(this.currentCard.cardType === "brand"){
        this.initSocial();
      }

任何 help/tips/suggestions 将不胜感激。

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();
};

并需要 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,
}))

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