想知道我是否可以为FormControl
(在组件中(添加占位符文本,这是我的代码:
(this.paramsForm.get(
'parameters'
) as FormArray).push(new FormControl('', [Validators.required]));
以 HTML 格式呈现
<div *ngFor="let formControl of paramsForm.get('parameters').controls">
<mat-form-field appearance="outline" class="block push-bottom">
<input
matInput
type="text"
[formControl]="formControl" //TODO
placeholder="formControl" //TODO
/>
</mat-form-field>
</div>
这里的答案很有帮助,但就我而言,拥有一个对象数组 ( {controlName:'name', value:'', placeholder: 'Enter name'}, {controlName:'email', value:'', placeholder: 'Enter email'}
( 来动态创建控件并为每个控件设置占位符文本。谢谢!
在@Eliseo注释之后,将占位符文本保留在数组中,结果如下:
我的 JSON 对象:
exportParameters
: [组标题:"导出组", 参数:[{键:'用户名', 值:'', 显示名称:"输入用户名'},键:"密码', 值:"'', 显示名称:"输入密码'} ]]
在组件.ts 中,创建了一个组
placeholders: string[] = [];
this.paramsForm = this.formBuilder.group({
exportParameters: this.fb.group({
parameters: this.fb.array([])
})
})
this.exportParameters.parameters.forEach( (param,index) => {
(this.paramsForm.get('exportParameters.parameters') as FormArray).push (new FormControl('',[Validatrors.required]));
this.placeholders[index] = param.displayName;
})
在组件的 HTML 中:
<div *ngFor="let formControl of paramsForm.get('exportParameters.parameters').controls; let i = index;">
<mat-form-field>
<input type="text" placeholder = "{{ placeholders[i] }}" />
</mat-form-field>
</div>
希望这对某人有所帮助!