【发布时间】:2021-08-26 07:03:56
【问题描述】:
我有由从 API 返回的人员对象数组组成的数据,我必须为此动态生成控件并在 Angular 12 应用程序中显示。返回的数据如下所示。
{
"type": "Person",
"fields": [
{
"name": "fullName",
"label": "Full name",
"validation": {
"rules": {
"required": true,
"maxLength": 100
},
"errorMessages": {
"required": "Enter your full name",
"maxLength": "The full name must not exceed 100 characters"
}
}
},
{
"name": "phoneNumber",
"label": "Phone number",
"validation": {
"rules": {
"required": true,
"maxLength": 16
},
"errorMessages": {
"required": "Enter a valid phone number",
"maxLength": "The phone number must not exceed 16 characters"
}
}
},
{
"name": "email",
"label": "Email",
"validation": {
"rules": {
"required": true,
"format": "^(([^<>()\\[\\]\\\\.,;:\\s@\"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"
},
"errorMessages": {
"required": "Enter a valid email address",
"format": "The email address must be valid"
}
}
}
],
"values": [
{
"id": 1,
"fullName": null,
"phoneNumber": null,
"email": null
},
{
"id": 2,
"fullName": "ytrytrytr",
"phoneNumber": null,
"email": null
},
{
"id": 3,
"fullName": "test",
"phoneNumber": "2353535",
"email": "dfdw@ww.com"
}
]
}
我的 component.ts 文件中用于获取数据并创建表单数组以及该表单数组中的表单组的代码如下所示。
form!: FormArray;
personFields!: any;
personValues: any;
ngOnInit() {
this.personService.getData()
.subscribe((res: FormObject) => {
this.personFields = res.fields;
this.personValues = res.values;
this.form = new FormArray(this.personValues.map((value:
any)=>this.createPersonData(value)));
});
}
createPersonData(person: any) {
let personFormGroup = new FormGroup({});
let validationsArray = [];
this.personFields.forEach(formField => {
validationsArray = [];
if(formField.validation.rules.required) {
validationsArray.push(Validators.required);
}
if(formField.validation.rules.maxLength) {
validationsArray.push(Validators.maxLength(formField.validation.rules.maxLength));
}
if(formField.validation.rules.format) {
validationsArray.push(Validators.pattern(formField.validation.rules.format));
}
let formFieldValue = person[formField.name] ? person[formField.name] : null;
personFormGroup.addControl(formField.name, new FormControl(formFieldValue,
validationsArray));
});
return personFormGroup;
}
现在,我正在绑定到模板 html 文件,如下所示,lib-input 和 lib-show-errors 是我在 Angular 库中拥有的组件我正在这个应用程序中使用。
<form *ngIf="form" [formGroup]="form">
<div *ngFor="let ohFormGroup of form.controls;let i=index">
<div [formGroup]="ohFormGroup">
<ng-container *ngFor="let formField of ohFormGroup.controls;">
<div>
<div class="label">{{formField.label}}</div>
<lib-input [formControlName]="formField.name">
</lib-input>
</div>
<lib-show-errors *ngIf="isSubmitted && ohFormGroup.controls[formField.name].errors"
[formField]="ohFormGroup.controls[formField.name]"
[errorMessages]="formField.validation.errorMessages">
</lib-show-errors>
</ng-container>
</div>
</div>
</form>
我需要显示对应于 JSON 中 values 数组中返回的人员对象的控件。例如,如果 values 数组有 4 个对象,我需要显示四组全名、电话号码和电子邮件控件,它们是 JSON 中 fields 数组的一部分。如果用户想通过单击按钮添加第五个人,我应该为这 3 个控件动态生成一个表单组并显示它,然后在提交表单时我需要将所有 5 个对象发布到 API POST 端点。这是我的要求。
我在使用上述模板时遇到错误。他们是
“FormArray”类型缺少“FormGroup”类型的以下属性:
“AbstractControl”类型缺少“FormGroup”类型的以下属性:controls、registerControl、addControl、removeControl 以及
行的另外 3 个属性行的“AbstractControl”类型上不存在属性“控件”
我不知道究竟是什么导致了上述问题。请帮我解决这个问题。
【问题讨论】:
您的 JSON 对象必须与您的 FormGroup 的结构完全匹配。现在没有。 我正在从 json 响应动态创建表单数组和表单组,然后绑定到模板。当我记录在控制台中创建的“表单”时,我可以看到表单数组和其中的表单组,但无法将它们绑定到模板。这就是我面临的问题。 我看到您的 JSON 中有数组,但您的代码中没有FormArrays 在 ngOnInit() 里面我有 this.form = new FormArray(this.personValues.map((value: any)=>this.createPersonData(value)));这是创建 FormArray 的地方。 当您应该使用 FormGroup 时,您正在将 FormArray 传递给您的
标签: angular angular-reactive-forms formarray formgroups