【问题标题】:How to access the properties of a formArray in HTML?如何在 HTML 中访问 formArray 的属性?
【发布时间】:2021-06-10 13:56:03
【问题描述】:

我正在尝试实现反应式 Angular 表单,但是,我无法访问 HTMLarray 的属性,如果有人可以指导我,我从未使用过反应式表单我会很感激!我正在使用 Angular 10 并且我有以下代码:

TS

operationModel: IScriptOperationsModel;
formOperation: FormGroup;

constructor(
  private fb: FormBuilder,
  ...
) {}

ngOnInit() {
  this.operationModel = new IScriptOperationsModel();
  this.operationModel.scriptOperationOrders = [];
  this.buildForm(new IScriptOperationsModel());

  this.scriptOperationsService.findScriptOperation(this.operationId).subscribe((operation) => {
    this.operationModel = operation.data as IScriptOperationsModel;  // api return
    this.buildForm(this.operationModel);  // I pass the return of the api to the form
  });
}

buildForm(operation: IScriptOperationsModel) {
  this.formOperation = this.fb.group({
    descriptionCode: [operation.descriptionCode],
    description: [operation.description],
    workStations: this.fb.array([])
  });
  this.formOperation.setControl('workStations', this.fb.array(this.operationModel.scriptOperationOrders));
}

get workStations(): FormArray {
  return this.formOperation.get('workStations') as FormArray;
}

HTML

<div
  class="card"
  [ngClass]="{'bg-principal': idx === 0, 'bg-alternative': idx !== 0}"
  formArrayName="workStations"
  *ngFor="let workstation of workStations.controls; index as idx"
>
  <div class="card-body" [formGroupName]="idx">
    <div class="form-row">
      <div class="form-group col-md-1">
        <label>Id Oper.</label>
        <input
          type="text"
          name="idOperation"
          class="form-control"
          disabled
          formControlName="rank"            <!-- whatever with or without binding gives error -->
        />
      </div>
      <div class="form-group col-md-2">
        <label>Time</label>
        <input
          type="time" class="form-control" name="defaultTime"
          [formControlName]="defaultTime"   <!-- whatever with or without binding gives error -->
        />
      </div>
    </div>
  </div>
</div>

型号

export class IScriptOperationsModel extends Serializable {
  public description: string;
  public descriptionCode: string;
  public scriptOperationOrders: IScriptOperationOrdersModel[];     // array which I need
}

export class IScriptOperationOrdersModel extends Serializable {
  public rank: number;
  public defaultTime: string;
  public asset?: IAssetModel;
  public provider?: IProviderModel;
}

error-handler.service.ts:87 错误:找不到带有路径的控件:'workStations -> 0 -> rank' @ undefined:undefined

注意:我已经在网站上查看了一些答案,例如 thisthisthis,但没有一个解决了这个问题!

【问题讨论】:

    标签: javascript angular typescript angular-reactive-forms


    【解决方案1】:

    你的问题在这里:

    this.formOperation.setControl(
    'workStations',
    this.fb.array(this.operationModel.scriptOperationOrders) <== here the problem
    );
    

    您传递的是 IScriptOperationOrdersModel 数组,而不是 表单组数组
    为了使您的代码正常工作,您必须循环 this.operationModel.scriptOperationOrders 数组的每个元素,并实例化一个 new FormControl 对象,然后将其推送到 workStations 表单数组中。
    要访问其元素,您可以使用controls[indexOfGroup].rate

    你可以看看this simple example你就明白了。

    【讨论】:

    • 感谢您的回答,可以在屏幕上添加表单字段了,现在我的问题如下,如何将此表单与API集成,就像用户第一次访问页面一样什么都没有,它会正常添加,但是,因为这是用户第二次访问屏幕,它如何显示之前已经添加的那些?
    • 不客气:) 老实说,我没有完全理解你的第二点,你的意思是:如果你想用相同的 formGroup 和 formArray 编辑你之前添加的实体怎么办,对吧?
    • 确切地说,例如,我如何使用来自 API 的数组构建表单,其数据格式与通知但来自后端的数据格式相同?正是在您的回复中,您说这是错误的:this.fb.array (this.operationModel.scriptOperationOrders)
    • 在这种情况下,您必须遵循与我上面的解决方案相同的逻辑,但通过添加我们将使用的“id”字段对您的 IScriptOperationOrdersModel 类进行一些更改用于每个更新元素的更新过程。但是,当您想从后端 API 检索数据时,您应该按照我上面提到的那样构建表单控件,然后将通过 .subscribe(..) 方法获取的数据设置为使用它的构建控件索引..如果这没有帮助或不清楚,如果您使用 [stackblitz.com](stackblitz.com/] 重现您的代码会很好
    猜你喜欢
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 2012-11-10
    • 2018-04-28
    • 2017-11-29
    • 2014-04-29
    相关资源
    最近更新 更多