javascript - FormArray 无效但不包含错误

标签 javascript angular angular6 angular-reactive-forms

我在 Angular 6 中使用了响应式(Reactive)形式。这种响应式(Reactive)形式包含一个名为 instrumentsFormGroup,它是一个 FormArray

我收到表单数组无效的信息,但数组的所有组件都是有效

有什么问题?

enter image description here

初始化表单

initializeForm(laboratory) {
    this.laboratoryForm = this.formBuilder.group({
      laboratoryId: [laboratory && laboratory.Id],
      clientId: [this.currentUserClientId],
      name: [laboratory && laboratory.Name, [Validators.required, Validators.maxLength(50)]],
      code: [laboratory && laboratory.Code, [Validators.required, Validators.maxLength(5)]],
      description: [laboratory && laboratory.Description, [Validators.required, Validators.maxLength(1000)]]
    });
    if (laboratory && laboratory.LaboratoryInfoSystem) {
      let laboratoryInfoSystemFormGroup = this.formBuilder.group({
        id: [laboratory.LaboratoryInfoSystem.Id],
        description: [laboratory.LaboratoryInfoSystem.Description, [Validators.maxLength(1000)]],
        model: [laboratory.LaboratoryInfoSystem.Model, [Validators.required, Validators.maxLength(50)]],
        name: [laboratory.LaboratoryInfoSystem.Name, [Validators.required, Validators.maxLength(50)]],
        lis_connection: this.formBuilder.group({
          id: [laboratory.LaboratoryInfoSystem.LaboratoryInfoSystemConnection.id],
          connection: [laboratory.LaboratoryInfoSystem.LaboratoryInfoSystemConnection.Connection, [Validators.required, Validators.maxLength(1000)]],
          connectionTypeId: [laboratory.LaboratoryInfoSystem.LaboratoryInfoSystemConnection.ConnectionTypeId, [Validators.required]],
          username: [laboratory.LaboratoryInfoSystem.LaboratoryInfoSystemConnection.Username, [Validators.maxLength(100)]],
          password: [laboratory.LaboratoryInfoSystem.LaboratoryInfoSystemConnection.Password, [Validators.maxLength(100)]]
        })
      });
      this.laboratoryForm.addControl('laboratoryInfoSystem', laboratoryInfoSystemFormGroup);
    }

    if (laboratory && laboratory.Instruments) {
      let formInstrumentsArray = laboratory.Instruments.map(function (instrument) {
        return this.getInstrumentForm(instrument.Model, instrument.Name, instrument.Description, instrument.InstrumentConnection);
      }.bind(this));
      this.laboratoryForm.addControl('instruments', this.formBuilder.array(formInstrumentsArray));
    }
  }

getInstrumentForm 函数

 getInstrumentForm(model, name, description, instrumentConnection): FormGroup {
return this.formBuilder.group({
  model: [model, [Validators.required, Validators.maxLength(50)]],
  name: [name, [Validators.required, Validators.maxLength(50)]],
  description: [description, [Validators.maxLength(1000)]],
  instrument_connection: this.formBuilder.group({
    id: [instrumentConnection && instrumentConnection.Id],
    connection: [instrumentConnection && instrumentConnection.Connection, [Validators.required, Validators.maxLength(1000)]],
    connectionTypeId: [instrumentConnection && instrumentConnection.ConnectionTypeId, [Validators.required]],
  })
});

最佳答案

请看stackblitz-demo .我将从以下内容开始,然后如果看到这已经解决了错误。

您有以下内容:

let formInstrumentsArray = laboratory.Instruments.map(function(instrument) {
  return this.getInstrumentForm(instrument.Model, instrument.Name, instrument.Description, instrument.InstrumentConnection);
}.bind(this));

缺少将映射的仪器变成表单数组内的表单控件。在我的示例中,我有这个:

const toppingsFGs = toppings.map(topping => this.fb.group(topping));

对于您来说,您需要弄清楚预处理,然后将其传递到组中的映射结果中。

let formInstrumentsArray = laboratory.Instruments.map(function(instrument) {
  return this.getInstrumentForm(instrument.Model, instrument.Name, instrument.Description, instrument.InstrumentConnection);
}.bind(this));


let final = formInstrumentsArray.map(instrument => this.fb.group(instrument));

关于javascript - FormArray 无效但不包含错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52538971/

相关文章:

angular - SheetJs 自动转换日期 Angular 7

javascript - ionic 5 : Is there a way to combine tabs and side menu

angular6 - ngx 数据表页脚图标不显示

angular - 如何在 Angular 6 中再次触发 HttpGet 请求?

javascript - Angular 6 背景图像交叉淡入淡出动画

javascript - JavaScript Azure SDK 中的 DefaultAzureCredential、InteractiveBrowserCredential 等由于未找到 child_process,在编译时会引发错误

javascript - 使用带有 2 个按钮的 jQuery 旋转图像

javascript - 在 IE7+ 中播放声音?

JavaScript 初始化没有经度和纬度的 Google map

javascript - 带下拉菜单的 Angular 过滤