【问题标题】:Dynamic Angular Forms: How to Set Input Width Dynamically for Each Control动态角度形式:如何为每个控件动态设置输入宽度
【发布时间】:2020-07-08 10:57:41
【问题描述】:

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

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

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

模板代码

<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
        }
    ];
}

【问题讨论】:

  • 尝试使用这个:[style.width]="form_elem.width".
  • 谢谢@developer033 我尝试了很多不同的技术和组合,这是唯一有效的。我把它放在 标签上,它完全符合我的要求 - 我会更新答案。

标签: angular angular-material angular-reactive-forms angular-forms angular-dynamic-components


【解决方案1】:

这似乎是唯一可行的方法,将 style 属性放在 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>

【讨论】:

    【解决方案2】:

    您可以使用 [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 }" />
    

    【讨论】:

    • 是的,我已经尝试过了,但是我在模板中遇到编译错误,其中 {{form_elem_name}} 在引号内或不在引号内 模板解析错误中的错误:解析器错误:得到插值 ({{}} ) 其中表达式应位于 [{'width':'{{ form_elem.width }}'}] 的第 10 列
    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    相关资源
    最近更新 更多