设置添加的动态输入的参数
Set parameters of an added dynamically input
我想动态添加输入并为每个添加的输入指定 formControlName 和 placeholder 值
这是我的观点:
当我点击 "plus" 时,我想得到这个结果:
每次单击加号按钮时我都设法添加了一个输入,但我无法指定占位符和 formControlName 值
这是我的 ts 代码:
addLink() {
//when the plus button is clicked
const placeholdervalue = this.addForm.get("placeholdervalue").value;
this.items = this.addForm.get("items") as FormArray;
this.items.push(this.createItem(placeholdervalue));
console.log(this.addForm.get("items"));
}
createItem(placeholdervalue: string): FormGroup {
let a = { [placeholdervalue]: "" };
return this.formBuilder.group(a);
}
ngOnInit() {
this.addForm = this.formBuilder.group({
items: this.formBuilder.array([]),
placeholdervalue: ""
});
}
}
这是我的观点:
<div class="row">
<div
class="col-md-3"
formArrayName="items"
*ngFor="
let item of addForm.get('items').controls;
let i = index
"
>
<div [formGroupName]="i">
<mat-form-field class="example-full-width">
<input
matInput
formControlName="" // i want to retrieve it from item
placeholder=""
/>
</mat-form-field>
</div>
</div>
</div>
这是我显示项目时得到的结果
据我了解,您的表单控件名称和占位符值相同。数组中的每个 FormGroup 都将包含一个 FormControl,其名称将是动态的。如果是这样,那么我们所要做的就是获取表单控件的名称。
我认为管道可以达到这个目的。试一试:
@Pipe({
name: 'getFormControlName'
})
export class GetFormControlNamePipe implements PipeTransform {
transform(value: FormGroup): string {
return Object.keys(value.controls)[0]
}
}
<div [formGroupName]="i">
<mat-form-field class="example-full-width">
<input
matInput
formControlName="{{item | getFormControlName}}"
placeholder="{{item | getFormControlName}}"
/>
</mat-form-field>
</div>
我想动态添加输入并为每个添加的输入指定 formControlName 和 placeholder 值
这是我的观点:
当我点击 "plus" 时,我想得到这个结果:
每次单击加号按钮时我都设法添加了一个输入,但我无法指定占位符和 formControlName 值
这是我的 ts 代码:
addLink() {
//when the plus button is clicked
const placeholdervalue = this.addForm.get("placeholdervalue").value;
this.items = this.addForm.get("items") as FormArray;
this.items.push(this.createItem(placeholdervalue));
console.log(this.addForm.get("items"));
}
createItem(placeholdervalue: string): FormGroup {
let a = { [placeholdervalue]: "" };
return this.formBuilder.group(a);
}
ngOnInit() {
this.addForm = this.formBuilder.group({
items: this.formBuilder.array([]),
placeholdervalue: ""
});
}
}
这是我的观点:
<div class="row">
<div
class="col-md-3"
formArrayName="items"
*ngFor="
let item of addForm.get('items').controls;
let i = index
"
>
<div [formGroupName]="i">
<mat-form-field class="example-full-width">
<input
matInput
formControlName="" // i want to retrieve it from item
placeholder=""
/>
</mat-form-field>
</div>
</div>
</div>
这是我显示项目时得到的结果
据我了解,您的表单控件名称和占位符值相同。数组中的每个 FormGroup 都将包含一个 FormControl,其名称将是动态的。如果是这样,那么我们所要做的就是获取表单控件的名称。
我认为管道可以达到这个目的。试一试:
@Pipe({
name: 'getFormControlName'
})
export class GetFormControlNamePipe implements PipeTransform {
transform(value: FormGroup): string {
return Object.keys(value.controls)[0]
}
}
<div [formGroupName]="i">
<mat-form-field class="example-full-width">
<input
matInput
formControlName="{{item | getFormControlName}}"
placeholder="{{item | getFormControlName}}"
/>
</mat-form-field>
</div>