您的问题是您创建了一个加载字段的方法,但您没有调用它,因此没有加载表单。
为此,您需要这样做:
在你的 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"
}
];
我希望这会有所帮助!