角动态形式和角材料

cco*_*ker 5 dynamic-forms angular-material angular

我在 Angular 5 中构建了一个动态表单组件(基于https://angular.io/guide/dynamic-form的文档和示例)。

一切正常,直到我尝试使用有角材料。

我在这里读过很多关于类似问题的文章,但它们似乎都是因为人们没有导入正确的模块或使用 mdInput 或 mat-Input 而不是 matInput。我遇到的问题并非如此。

任何想法或建议将不胜感激。

更改代码 - 因错误而中断 -

*mat-form-field 必须包含 MatFormFieldControl。***

动态表单控件组件模板

我对下面的工作代码所做的唯一更改是将输入字段包装起来并将 matInput 属性添加到输入字段。

我通过核心模块导入所有材质模块(MatFormFieldModule 和 MatInputModule 等)。我的所有材质输入和表单字段都在应用程序中的所有其他组件中工作,所以我不认为问题是我在导入中丢失了任何内容。

<div [formGroup]="form">

    <div [ngSwitch]="control.controlType">
        <mat-form-field>
            <input matInput placeholder="{{control.label}}" *ngSwitchCase="'textbox'" [formControlName]="control.key" [id]="control.key"
                [type]="control.type">
        </mat-form-field>
        <select [id]="control.label" *ngSwitchCase="'dropdown'" [formControlName]="control.key">
            <option value="">Select {{control.label}}</option>
            <option *ngFor="let opt of control.options" [value]="opt.key">{{opt.value}}</option>
        </select>
    </div>

    <div class="errorMessage" *ngIf="!isValid">{{control.label}} is required</div>
</div>
Run Code Online (Sandbox Code Playgroud)

当前代码-这工作完美,但我没有得到角度材料格式

选择器

<mi-dynamic-form [controls]="controls"></mi-dynamic-form>
Run Code Online (Sandbox Code Playgroud)

动态表单组件

    import { Component, Input } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

import { DynamicFormBase } from './dynamic-form-base';

@Component({
    selector: 'mi-control',
    templateUrl: './dynamic-form-control.component.html'
})
export class DynamicFormControlComponent {

    // API
    @Input() control: DynamicFormBase<any>;
    @Input() form: FormGroup;

    get isValid() { return this.form.controls[this.control.key].valid; }
}
Run Code Online (Sandbox Code Playgroud)

动态表单组件模板

    <div [formGroup]="form">
    <div [ngSwitch]="control.controlType">
            <input  placeholder="{{control.label}}" *ngSwitchCase="'textbox'" [formControlName]="control.key" [id]="control.key"
                [type]="control.type">
        <select [id]="control.label" *ngSwitchCase="'dropdown'" [formControlName]="control.key">
            <option value="">Select {{control.label}}</option>
            <option *ngFor="let opt of control.options" [value]="opt.key">{{opt.value}}</option>
        </select>
    </div>

    <div class="errorMessage" *ngIf="!isValid">{{control.label}} is required</div>
</div>
Run Code Online (Sandbox Code Playgroud)

动态表单控制组件

import { Component, Input } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

import { DynamicFormBase } from './dynamic-form-base';

@Component({
    selector: 'mi-control',
    templateUrl: './dynamic-form-control.component.html'
})
export class DynamicFormControlComponent {

    // API
    @Input() control: DynamicFormBase<any>;
    @Input() form: FormGroup;

    get isValid() { return this.form.controls[this.control.key].valid; }
}
Run Code Online (Sandbox Code Playgroud)

动态表单控件组件模板

 <div [formGroup]="form">
        <!-- <label [attr.for]="control.key">{{control.label}}</label> -->

        <div [ngSwitch]="control.controlType">
            <mat-form-field>
                <input matInput placeholder="{{control.label}}" *ngSwitchCase="'textbox'" [formControlName]="key" [id]="control.key"
                    [type]="control.type">
            </mat-form-field>
            <mat-select [id]="control.label" *ngSwitchCase="'dropdown'" [formControlName]="control.key">
                <mat-option value="">Select {{control.label}}</mat-option>
                <mat-option *ngFor="let opt of control.options" [value]="opt.key">{{opt.value}}</mat-option>
            </mat-select>
        </div>

        <div class="errorMessage" *ngIf="!isValid">{{control.label}} is required</div>
    </div>
Run Code Online (Sandbox Code Playgroud)

小智 0

我确信你已经解决了这个问题。mat-form-field 的错误必须包含 MatFormFieldControl。表示所使用的材料成分不是进口的。即使用 mat-button 指令时需要导入 MatButtonModule。