angular 2:通过FormControl获取FormGroup

angular 2: get FormGroup by FormControl

我可以获得 FormGroup - 我拥有的 FormControl 的父级吗? 像这样:

    onBlur(formControl: FormControl) {
    var formGroup = formControl.parent // a FormGroup has the FormControl
    if (formControl.dirty) {

        console.log(formControl);
    }

}

您无法访问 FormControl 的父级(参见:https://github.com/angular/angular/issues/5635)。但是您可以使用模型驱动表单访问父级。

 constructor(private fb: FormBuilder) {

 this.form = fb.group({
   name: [],
   address: fb.group({
     city: ['', Validators.required],
     street: ['', Validators.required],
     zipCode: '',
     year: 2016
   }),
   groupDates: fb.group({
     fromDate_g: [''],
     toDate_g: ['', Validators.required]
   }, {validator: Validators.required}),
   dates: fb.group({
     fromDate: '',
     toDate: ''
   })
  });

  let datesGroup =  this.form.controls['dates'] as FormGroup;


}

这似乎是可能的,但违背了惯例。

 getParentForm(formControl: FormControl): FormGroup {
    return formControl['_parent'];
}

我目前有 angular 5,但我确定在 angular 4 上它也像这样工作:

formControl.parent

例如,在我的代码中,我正在从同一组中获取其他控件(按名称):

formControl.parent.get(neighbourName)