Angular 具有来自 JSON 的元素组的动态表单

Angular dynamic form with groups of elements getting from JSON

我正在 angular 申请和构建 angular 动态表单。

这里我试图将表格分成两部分,例如人名和个人详细信息..

对于个人姓名,它适用于分组,但对于个人详细信息,它不起作用。

Html:

<div *ngIf="form">
  <div *ngFor="let question of questions" class="form-row" [formGroup]="form">
      <ng-container *ngIf="!question.children">
        <app-question [question]="question" [form]="form"></app-question>
      </ng-container>
      <ng-container *ngIf="question.controlType === 'group' && question.children && question.children.length > 0">
        <app-dynamic-group [questions]="question.children" [form]="form.controls[question.key]" [key]="question.key" [formControlName]="question.key"></app-dynamic-group>
      </ng-container>
  </div>
</div>

JSON:

  jsonData: any = [
    {
      "elementType": "group",
      "key": "person_name",
      "children": [
        {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "first_name",
          "label": "First Name",
          "type": "text",
          "value": "",
          "required": true,
          "minlength": 3,
          "maxlength": 20,
          "order": 1
        },
        {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "last_name",
          "label": "Last Name",
          "type": "text",
          "value": "",
          "required": true,
          "order": 2
        }
     ],
    },
        {
      "elementType": "group",
      "key": "personal_details",
      "children": [
        {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "email",
          "label": "Email",
          "type": "text",
          "value": "",
          "required": true,
          "minlength": 3,
          "maxlength": 20,
          "order": 1
        },
        {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "mobile",
          "label": "Mobile",
          "type": "text",
          "value": "",
          "required": true,
          "order": 2
        }
     ],
    },
  ];

工作中的 Stckblitzhttps://stackblitz.com/edit/angular-x4a5b6-5uj52y

工作时一切正常..已经为人名创建了一个组并且工作正常但是对于个人详细信息我找不到输入框..

单个表格需要拆分,每个部分上方都有标题,这就是此表格的要求。

这里 {{question.key}} 在每个输入框上显示名称,但我只需要在顶部显示 Person Name.. 因为它是 parent 标题,其余的如 First Name, Last Name 是输入框标签.. 如何在每个部分的前面单独显示 parent 标题 (Person Name (Has First and Last Name) , Personal Details (有电子邮件和手机))...

我希望按照下面的方式拆分订单,并分别为每个订单添加标题。

人名

 -> First Name
 -> Last Name

个人资料

 -> Email
 -> Mobile Number

如果我对上述方法有误,请帮助我拆分此 https://stackblitz.com/edit/angular-x4a5b6-geesde 动态表单,如下面给出的方法..

我的表单需要看起来像这样 https://stackblitz.com/edit/angular-zc34qr 但它需要是纯 angular 动态表单和 JSON 加载..

请帮助我创建一个群组 Personal Details,就像已经创建并正在运行的 Person Name 一样。

卡在这个问题上好久了,请帮助我...

我不明白你为什么要在这里创建额外的 formGroup:

this.form = new FormGroup({main: innerForm});

只需使用您从服务中获取的 formGroup:

动态-form.component.ts

this.form = this.qcs.toFormGroup(this.questions);

动态-form.component.html

<app-dynamic-group [questions]="questions" [form]="form"></app-dynamic-group>

现在,您不需要在 DynamicGroupComponent 上实现 ControlValueAccessor。您将 FormGroup 传递给它,它应该足以动态生成表单。

动态-group.component.ts

@Component({
  selector: 'app-dynamic-group',
  templateUrl: './dynamic-group.component.html'
})
export class DynamicGroupComponent {
  @Input() form: FormGroup;

  @Input() questions: QuestionBase<any>[] = [];
}

动态-group.component.html

<div *ngFor="let question of questions" class="form-row">
    <app-question *ngIf="!question.children" [question]="question" [form]="form"></app-question>

    <app-dynamic-group 
       *ngIf="question.controlType === 'group' && question.children && question.children.length"
       [form]="form.get(question.key)"
       [questions]="question.children">
    </app-dynamic-group>
</div>

Forked Stackblitz