有人可以帮我用从http调用(按需)加载的数据来创建此动态表单吗? 没有按需数据的代码的工作示例在这里 https://stackblitz.com/edit/github-gaztsv
,我尝试按一下按钮即可加载数据,但这种方法不起作用,请在下面找到它 https://stackblitz.com/edit/github-gaztsv-1azc56?file=src%2Fapp%2Fcomponents%2Fdynamic-form%2Fdynamic-form.component.ts
答案 0 :(得分:0)
您的问题是您创建了一种用于加载字段的方法,但是您没有调用它,因此未加载表单。 为此,您需要执行以下操作: 在您的app.component.ts上,您需要
implements OnInit
,您需要在
上加载方法ngOnInit(){
this.loadvalues()
}
像这样加载组件时,角形将填充您的表单!
如果您希望通过按钮执行此操作,请执行以下操作:
例如,在您的app.component.ts上获取一个布尔变量,您可以这样做:visible: boolean = false;
然后loadvalues(){ this.visible = true; }
格式为:<dynamic-form [fields]="regConfig" (submit)="submit($event)" *ngIf="visible"> </dynamic-form>
如果您想显示和隐藏在同一按钮中,请执行以下操作:
loadvalues(){
if(this.visible)
{
this.visible = false;
}else{
this.visible = true;
}
}
另外,您需要在顶部加载字段。
regConfig: FieldConfig[] = [
{
type: "input",
label: "Username",
inputType: "text",
name: "name",
validations: [
{
name: "required",
validator: Validators.required,
message: "Name Required"
},
{
name: "pattern",
validator: Validators.pattern("^[a-zA-Z]+$"),
message: "Accept only text"
}
]
},
{
type: "input",
label: "Email Address",
inputType: "email",
name: "email",
validations: [
{
name: "required",
validator: Validators.required,
message: "Email Required"
},
{
name: "pattern",
validator: Validators.pattern(
"^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$"
),
message: "Invalid email"
}
]
},
{
type: "input",
label: "Password",
inputType: "password",
name: "password",
validations: [
{
name: "required",
validator: Validators.required,
message: "Password Required"
}
]
},
{
type: "radiobutton",
label: "Gender",
name: "gender",
options: ["Male", "Female"],
value: "Male"
},
{
type: "date",
label: "DOB",
name: "dob",
validations: [
{
name: "required",
validator: Validators.required,
message: "Date of Birth Required"
}
]
},
{
type: "select",
label: "Country",
name: "country",
value: "UK",
options: ["India", "UAE", "UK", "US"]
},
{
type: "checkbox",
label: "Accept Terms",
name: "term",
value: true
},
{
type: "button",
label: "Save"
}
];
我希望这会有所帮助!
答案 1 :(得分:0)
这是一篇旧文章,但可能会帮助使用json处理动态表单的人 这可能会帮助某人 https://github.com/saqibumar/angular-6-dynamic-form-using-material/!