找不到名称为的控件:

时间:2019-01-30 13:39:44

标签: angular angular-forms reactive-forms angular-formbuilder

我有一个非常复杂的表格,我想分解成各个部分。这是我的基本表单(仅用于示例字段),我正在使用FormBuilder

ngOnInit() {
  this.predictorQuestion = this.fb.group({
  question: ['', Validators.required],
  options: this.fb.array([
    this.fb.control('', Validators.required),
  ]),
  meta_options: this.fb.group({
   test_type: ['', Validators.required],
  })
});

get meta_options() {
  return this.predictorQuestion.get('meta_options') as FormGroup;
}

get options() {
  return this.predictorQuestion.get('options') as FormArray;
}

如果我尝试将其连接到模板,则效果很好:

<form [formGroup]="predictorQuestion" fxLayout="column">
  <mat-form-field fxFlex appearance="outline">
    <mat-label>Question</mat-label>
    <input matInput formControlName="question">
  </mat-form-field>

  <div fxLayoutAlign="space-between center">
    <h3>Options</h3>
    <button (click)="addOption()" matTooltip="Add option" matTooltipPosition="right" mat-mini-fab type="button">
      <mat-icon>add</mat-icon>
    </button>
  </div>
  <div formArrayName="options" fxLayout="column">
    <div *ngFor="let answer of options.controls; let i = index" fxLayout="row" fxLayoutAlign="space-between stretch">
      <mat-form-field appearance="outline" fxFlex>
        <mat-label>Option {{ i+1 }} </mat-label>
        <input fxFlex matInput [formControlName]="i">
      </mat-form-field>
      <button mat-icon-button matTooltip="Remove this option" matTooltipPosition="right" (click)="removeOption(i)">
        <mat-icon>close</mat-icon>
      </button>
    </div>
  </div>

  <div formGroupName="meta_options" fxLayoutAlign="space-between" fxLayoutGap="20px" fxLayout="column">
    <mat-form-field fxFlex="25">
      <mat-select formControlName="test_type">
        <mat-option *ngFor="let vtype of vtypes" value="{{ vtype.value }}">{{ vtype.name }}</mat-option>
      </mat-select>
    </mat-form-field>
  </div>
</form>

此渲染没有任何错误。

如果我尝试通过以下方式将meta_options.test_type分解成一个单独的组件:

component.ts

@Input() parent_form: FormGroup;
public vtypes: Array<Object>;


  constructor(private fb: FormBuilder) {
    this.vtypes = [
      {
        name: 'Timestamp',
        value: 'timestamp'
      },
      {
        name: 'Over',
        value: 'over'
      }
    ];
  }

component.html

<mat-form-field fxFlex="25" [formGroup]="parent_form">
  <mat-select formControlName="test_type">
    <mat-option *ngFor="let vtype of vtypes" value="{{ vtype.value }}">{{ vtype.name }}</mat-option>
  </mat-select>
</mat-form-field>

并在我的主要父表单中将该组件用作

<meta-option-value [parent_form]="predictorQuestion"></meta-option-value>

我收到以下错误:

"Cannot find the control with the name: 'test_type'"

我在这里想念什么?

3 个答案:

答案 0 :(得分:1)

您正在将完整的FormGroup (predictorQuestion)传递给孩子。您只需将predictorQuestion.get('meta_types')作为parent_form传递到[parent_form]="predictorQuestion.get('meta_types')"

答案 1 :(得分:0)

通过“控制”命令并在孩子中使用[FormControl]

<meta-option-value [formControl]="predictorQuestion.get('meta_options')">
</meta-option-value>

您的元选项

<mat-form-field fxFlex="25" [formControl]="formControl">
  ...

</mat-form-field>

//and add the Input
@Input()formControl: FormControl;

如果您需要传递FormGroup或Form Array,同样的想法也可以

答案 2 :(得分:0)

通过定义formControlName="test_type",您可以说您的子组件根据父容器使用viewProviders而不管容器的类型(FormGroupName,FormGroup或FormArray):

meta-option-value.component.ts

@Component({
  selector: 'meta-option-value',
  template: `
    <mat-form-field fxFlex="25">
      <mat-select formControlName="test_type">
        <mat-option ...>{{ vtype.name }}</mat-option>
      </mat-select>
    </mat-form-field>
  `,
  viewProviders: [{
    provide: ControlContainer,
    useFactory: (container: ControlContainer) => container,
    deps: [[new SkipSelf(), ControlContainer]],
  }]
})
export class MetaOptionValueComponent {
  ...
}

parent.template.html

<div formGroupName="meta_options">
                     /\
                     ||
              will look at this group   
    <meta-option-value></meta-option-value>
</div>

如您所见,MetaOptionValueComponent仅包含其自身的相关逻辑。

Stackblitz Example

另请参阅:

当然,另一种方法是传递FormGroup或FormControl实例,并按照其他答案中的建议直接使用它

相关问题