我有一个FormArray,有3个FormGroup,每个FormGroup有2个控件.我不知道如何访问表单组中的控件来设置表单控件名称.

设置表单

this.form = this.formBuilder.group({
---a couple controls---
properties: this.formBuilder.array({
  first: this.formBuilder.group({ 
   label: new UntypedFormControl(),
   value: new UntypedFormControl(),
  }),
  second: this.formBuilder.group({ 
   label: new UntypedFormControl(),
   value: new UntypedFormControl(),
  }),
  third: this.formBuilder.group({ 
   label: new UntypedFormControl(),
   value: new UntypedFormControl(),
  }),
})
})

数组属性的getter

get properties() {
  return this.form.controls.properties as FormArray;
}

我可以添加和删除一行属性.

html的形式.我不确定如何从表单数组访问每个表单组中的标签和值控件.

<div formArrayName="properties">
  <ng-container *ngFor="let propertiesForm of properties.controls; let i = index">
  <div [formGroupName]="i">
---then I have 3 sets of mat form fields each with and label and value---
  </div>
  </ng-container>
</div>

推荐答案

this.formBuilder.array()方法只接受一个数组参数.你不能为它提供对象.

Approach 1: With 100

  1. properties struct 修改为array.
  2. 你可以使用一个循环来添加你需要的数字FormGroupproperties FormArray.你应该考虑实现一个函数来生成单个属性FormGroup.这旨在减少代码冗余.
ngOnInit() {
  // Form with Form Array
  this.form = this.formBuilder.group({
    //---a couple controls---
    properties: this.formBuilder.array([]),
  });

  for (let i = 0; i < 3; i++)
    this.properties.push(this.createPropertyFormGroup());
}

createPropertyFormGroup() {
  return this.formBuilder.group({
    label: new UntypedFormControl(),
    value: new UntypedFormControl(),
  });
}
<form [formGroup]="form">
  <div formArrayName="properties">
    <ng-container
      *ngFor="let propertiesForm of properties.controls; let i = index"
    >
      <div [formGroupName]="i">
        <label>Label: </label>
        <input formControlName="label" />
        <br />

        <label>Value: </label>
        <input formControlName="value" />
      </div>
      <hr />
    </ng-container>
  </div>
</form>

Approach 2: With 100

  1. 在HTML中,你需要KeyValuePipe来重新定义属性FormGroup对象并生成每个FormGroup.
this.form = this.formBuilder.group({
  //---a couple controls---
  properties: this.formBuilder.group({
    first: this.createPropertyFormGroup(),
    second: this.createPropertyFormGroup(),
    third: this.createPropertyFormGroup(),
  }),
});

get propertiesFormGroup() {
  return this.form.controls['properties'] as FormGroup;
}
<form [formGroup]="form">
  <div formGroupName="properties">
    <ng-container
      *ngFor="let propertiesForm of propertiesFormGroup.controls | keyvalue;"
    >
      <div [formGroupName]="propertiesForm.key">
        <label>Label: </label>
        <input formControlName="label" />
        <br />

        <label>Value: </label>
        <input formControlName="value" />
      </div>
      <hr />
    </ng-container>
  </div>
</form>

Demo @ StackBlitz

Angular相关问答推荐

Angular 信号效果,try 重置表单并获取在计算或效果中不允许写入信号"

如何在自定义验证器中获取所有表单控件(字段组动态创建)

对子router-outlet 应用主机样式

如何使用ANGLING HTTP.POST并将类型更改为键入的回复?

NGX- colored颜色 Select 器安装出错

我正在try 将一个带有模块联盟的Angular 8项目expose 到另一个Angular 项目,Angular 是15,这可能吗?如果是这样,有什么建议吗?

Angular 组件存储,为什么我的效果可以';在http错误后不会被触发?

使用 Angular 类构造函数来获取依赖项

ngrx 效果等到从初始值以外的其他状态收到响应

大型 Angular 12 应用程序 - 我如何管理 js 文件

Angular:为什么在我的自定义 ErrorHandler 中订阅主题不触发?

如何在数据表angular material中显示空消息,如果未找到数据

如何在 Angular 5 HttpInterceptor 中添加多个标头

如何在 Angular 2 应用程序中配置不同的开发环境

如何将新的 FormGroup 或 FormControl 添加到表单

在 Angular 2 中订阅路由参数和查询参数

Angular 2在单击子项时防止单击父项

读取文件并解析其内容

Failed to execute 'setAttribute' on 'Element': ']' is not a valid attribute name

Angular2中是否有像window.onbeforeunload这样的生命周期钩子?