我不知道这是否可能,但我已经构建了一个运行良好的动态表单组件,我可以通过配置文件控制许多项目。
我无法控制的一项是输入字段的宽度,我尝试了十几种方法,但似乎都没有。即在创建表单控件等时在组件中设置宽度
有谁知道我正在尝试做的事情是否真的可行并且可以为我指明正确的方向。
模板代码
<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
}
];
}