代码之家  ›  专栏  ›  技术社区  ›  Ishan Khare

找不到名为的控件

  •  0
  • Ishan Khare  · 技术社区  · 6 年前

    我有一个非常复杂的表格,我想把它分解成各个部分。这是我的基本表单(仅示例字段),我正在使用 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'
          }
        ];
      }
    

    组件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 回复  |  直到 6 年前
        1
  •  1
  •   Sachin Gupta    6 年前

    你通过了完整的 FormGroup (predictorQuestion) 给孩子。你只需要通过 predictorQuestion.get('meta_types') parent_form 作为 [parent_form]="predictorQuestion.get('meta_types')" .

        2
  •  0
  •   Eliseo    6 年前

    将“控件”传递给您的孩子,并在他们中使用[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数组,同样的想法也会起作用。

        3
  •  0
  •   yurzui    6 年前

    您可以说您的子组件要使用 formControlName="test_type" 通过定义 viewProviders :

    元选项值.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 {
      ...
    }
    

    父级.template.html

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

    如您所见,metaoptionvaluecomponent只包含它自己的相关逻辑。

    Stackblitz Example

    参见:

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