2017-07-20 9 views
2

私のケースでは特定のタイプの特定のフィールドに条件付きの全体フォームにformGroupを追加しようとしています。このformGroupを追加するMy機能は、次のようになります。「社会は」「」期待されていると言った後は条件付きで別のformGroupにformGroupを追加します。angular2

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), 
      }) 
     ) 
    } 

は、今私は、大腸の下typescriptですエラーを取得しています。私はaddControl()が機能するかどうかを調べるためにこれをテストすることができませんでした。 initSocialを発射する

My機能はngOnInitにあり、次のようになります。

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

すべてのヘルプ/ヒント/提案をいただければ幸いです。

+0

それをしません'' social''の周りに '{}'がありません。 「なぜこのコードがうまくいかないのですか」という質問は投稿しないでください。 – cgTag

答えて

1

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つの引数を期待:制御、カンマで区切って。 それでは、セミコロン()を交換しようとカンマ()と '社会' の後:

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)を必要としない、フォームビルダは、あなたはすでに

+0

そう!それは痛いほど簡単でした。これにより、関数が正しく機能しています。ありがとう@アントリー! –

関連する問題