2017-07-13 4 views
1


私は自分のプロジェクトを整理しようとしていますが、フォームgroupeを使用しています。コンポーネントが混乱しています。別のクラスにフォームビルダーを配置する方法があるかどうかを知りたい私の主なコンポーネントの中でそれを呼び出す。
私はリアクティブフォームを使用しています。
私はそのすべてが
フォームを別のファイルに配置する方法4

コンストラクタ(民間のhttp:HTTP、プライベート_filterService:FiltreAdvancedSearchService)ありがとう宣言避けるためにしようとしている{ this.loading =はtrue。

this._filterService.getAllData().subscribe(res => { 
    this.Recherche = res; 
    this.loading = false; 
    this.status = 304; 

}, err => { 
    this.status = err.status; 
    console.log(this.status) 
}); 
this.SearchForm = new FormGroup({ 
    start_date: new FormControl('', [Validators.required ]), 
    end_date: new FormControl('', [Validators.required ]), 
    n_doc: new FormControl('', [ Validators.pattern('^[0-9]+$') ]), 
    pw: new FormControl('', [Validators.pattern('^[0-9]+$') ]), 
    li: new FormControl('', [Validators.pattern('^[0-9]+$') ]), 
    fac: new FormControl('', [Validators.pattern('^[0-9]+$') ]), 
    cont: new FormControl('', [Validators.pattern('/^[a-zA-Z]$/') ]), 
    type: new FormControl(this.types), 
    cat: new FormControl(this.categories), 
    immat: new FormControl(), 
    ser: new FormControl(''), 
    mod: new FormControl(''), 
    en: new FormControl(this.energies), 
    client_num: new FormControl(), 
    client_nom: new FormControl(), 
    reg: new FormControl(), 

}); 

}

+0

それはあなたのコードが – umar

答えて

0

はい、それは例えばngOnInit()関数の外側フォームビルダーを置くことが可能です。

ngOnInit(){ 
blablabla ... 
this.buildform(); 
} 

buildForm() { 
     this.form = this.fb.group({ 
      Id: [{ value: '', disabled: true }, Validators.required], 
      Insertions: [{ value: '' }, Validators.required], 
      IdForm: [{ value: '', disabled: !this.is_edit }, Validators.required], 
      price: ['', Validators.required] 
     }); 
    } 
+0

を最適化することができます方法を特定するのは簡単だように、あなたのコードを投稿したばかりの私の一日保存された、ありがとうございます! –

1

このような静的関数を持つ注入可能なサービスクラスを作成します。別のフォームに関数を追加する。

import {Injectable} from "@angular/core"; 
import {FormBuilder, FormGroup, Validators} from "@angular/forms"; 
@Injectable() 
export class FormHelper { 

    constructor(private fb: FormBuilder) {} 

    buildForm1(entityName: string): boolean { 
    return this.fb.group({ 
     'firstName': [null, [Validators.required]], 
     'secondName': [null, [Validators.minLength(10), Validators.maxLength(2000)]] 
    }); 
    } 
} 
関連する問題