Angular 用户输入角度为6的动态反应表单

Angular 用户输入角度为6的动态反应表单,angular,angular-reactive-forms,Angular,Angular Reactive Forms,如何根据输入表单用户(下拉选择)添加表单字段,该用户可以有输入、按钮、,角度反应表单中的复选框等。您基本上可以从下拉列表中检查所选选项的值,并使用*ngIf显示其他字段,或者不基于此显示其他字段。我认为这就是您所要求的……不使用下拉选择,而是动态地将字段添加到表单中 请参见下面的示例 app.component.ts 从'@angular/core'导入{Component}; 从“@angular/forms”导入{FormBuilder、FormControl、FormGroup、Vali

如何根据输入表单用户(下拉选择)添加表单字段,该用户可以有输入、按钮、,角度反应表单中的复选框等。

您基本上可以从下拉列表中检查所选选项的值,并使用*ngIf

显示其他字段,或者不基于此显示其他字段。我认为这就是您所要求的……不使用下拉选择,而是动态地将字段添加到表单中

请参见下面的示例

app.component.ts

从'@angular/core'导入{Component};
从“@angular/forms”导入{FormBuilder、FormControl、FormGroup、Validators};
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
名称='角度';
表格:表格组;
newFieldName:string;
ControlName:string[]=[];
pageMessage=“”;
构造函数(私有fb:FormBuilder){
this.form=fb.group({})
}
addToForm(事件){
console.log(this.newFieldName);
const ctrl=new FormControl();
ctrl.validator=Validators.required;
ctrl.setValue(空);
this.form.addControl(this.newFieldName,ctrl)
this.controlNames.push(this.newFieldName);
this.newFieldName='';
}
submitForm(){
如果(!this.form.valid){
this.pageMessage=“无效表单”
}    
this.pageMessage='';
console.log(this.form.value)
}
}
app.component.ts


开始编辑以查看发生的奇迹:)

输入输入名称: 进入
{{pageMessage}} {{c}} 提交表格
您可以使用formArray动态推送FormControl或FormGroup