0

我不知道这是否可能,但我已经构建了一个运行良好的动态表单组件,我可以通过配置文件控制许多项目。

我无法控制的一项是输入字段的宽度,我尝试了十几种方法,但似乎都没有。即在创建表单控件等时在组件中设置宽度

有谁知道我正在尝试做的事情是否真的可行并且可以为我指明正确的方向。

模板代码

<div fxLayout="row">
    <div [formGroup]="formGroup" fxLayoutAlign="start" fxLayoutGap="10px">
        <div *ngFor="let form_elem of formTemplate">
            <div *ngIf="form_elem.visible === 'true'">
                <mat-form-field>
                    <mat-label>{{ form_elem.label }}</mat-label>
                    <input
                        matInput
                        type="text"
                        formControlName="{{ form_elem.name }}"
                        class="{{ form_elem.width }} "
                    />
                </mat-form-field>
            </div>
        </div>
    </div>
</div>

打字稿代码

import {
    ChangeDetectionStrategy,
    Component,
    EventEmitter,
    Input,
    Output
} from "@angular/core";
import { FormGroup, FormControl } from "@angular/forms";

@Component({
    selector: "fw-form-array-item",
    templateUrl: "./form-array-item.component.html",
    styleUrls: ["./form-array-item.component.scss"],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class FormArrayItemComponent {
    @Output() remove = new EventEmitter<FormGroup>();
    @Input() formGroup: FormGroup;
    @Input() formTemplate: any;

    constructor() {}

    ngOnInit() {
        let group = {};
        this.formTemplate.forEach(input_template => {
            group[input_template.label] = new FormControl('');
        });
    }
}

表单模板配置文件

export class OrdersProductsFormTemplate {
    config = [
        {
            type: "textBox",
            label: "id",
            name: "id",
            width: "50%",
            justify: "left",
            visible: 'true',
            disabled: true
        },
        {
            type: "textBox",
            label: "Name",
            name: "name",
            width: "100%",
            justify: "left",
            visible: 'true',
            disabled: false
        },
        {
            type: "textBox",
            label: "Currency",
            name: "currency",
            width: "100%",
            justify: "left",
            visible: 'true',
            disabled: false
        },
        {
            type: "textBox",
            label: "Retail",
            name: "retailPrice",
            width: "100%",
            justify: "left",
            visible: 'true',
            disabled: false
        },
        {
            type: "textBox",
            label: "Supplier Price",
            name: "supplierPrice",
            width: "100%",
            justify: "left",
            visible: 'true',
            disabled: false
        }
    ];
}
4

2 回答 2

0

您可以使用带有属性宽度的 [ngStyle] 添加内联宽度,例如

<input matInput type="text"formControlName="{{ form_elem.name }}"
      [ngStyle]="{'width':'{{ form_elem.width }}'}" />

或这个

<input matInput type="text"formControlName="{{ form_elem.name }}"
      [ngStyle]="{'width': form_elem.width }" />
于 2020-03-27T16:57:57.593 回答
0

这似乎是唯一可行的方法,将样式属性放在 mat-form 字段标记上,并在没有插值的情况下引用 form_elem.width 属性。

更正的模板代码

<div fxLayout="row">
    <div [formGroup]="formGroup" fxLayoutAlign="start" fxLayoutGap="10px">
        <div *ngFor="let form_elem of formTemplate">
            <div *ngIf="form_elem.visible === 'true'">
                <mat-form-field [style.width]="form_elem.width">
                    <mat-label>{{ form_elem.label }}</mat-label>
                    <input
                        matInput
                        type="text"
                        formControlName="{{ form_elem.name }}"

                    />
                </mat-form-field>
            </div>
        </div>
    </div>
</div>
于 2020-03-28T03:33:22.753 回答