简体   繁体   English

如何在Angular反应形式的FormGroup中的FormArray上进行迭代

[英]How to iterate over FormArray in FormGroup in an Angular reactive Form

The Structure of my form is as shown below 我的表格的结构如下所示

-MainForm(ParentForm)
 -FormGroup(MedicineGroup)
  -FormArray(MedicineArray)

I wanted to iterate MedicineArray for which, i did some research and wrote the below code 我想迭代MedicineArray为此,我做了一些研究并编写了以下代码

for (let control of soForm.get('MedicineGroup').controls['MedicineArray'].controls) {
    medObj.name.push(control.controls['MedName'].value);
  }

The code is working fine but i am getting a warning, which says 该代码工作正常,但我收到警告,说

Property 'controls' does not exist on type 'AbstractControl'.

Is there any other or better way to iterate a FormArray which is inside a FormGroup? 还有其他更好的方法可以迭代FormGroup内部的FormArray吗?

You can try using ['controls'] instead of .controls : 您可以尝试使用['controls']而不是.controls

for (let control of soForm.get('MedicineGroup')['controls']['MedicineArray']['controls']) {
    // ...
}

Another option would be: 另一种选择是:

const formArray = soForm.get('MedicineGroup.MedicineArray') as FormArray;
console.log(formArray);

It's a good practice to access nested controls using dot notation. 使用点表示法访问嵌套控件是一个好习惯。

The reason this is occurring is because the type system understands that you have an AbstractControl, because AbstractControl.prototype.get() returns an AbstractControl , but it doesn't know which concrete AbstractControl you are referencing, so accessing a controls property causes this warning. 发生这种情况的原因是因为类型系统理解您具有AbstractControl,因为AbstractControl.prototype.get()返回了AbstractControl ,但是它不知道您要引用哪个具体的AbstractControl,因此访问controls属性会导致此警告。 。

You have some options as to how to solve it, casting or introducing another symbol, but the method is the same: provide better type information: 您可以选择如何解决,强制转换或引入另一个符号的方法,但是方法是相同的:提供更好的类型信息:

let aFormArray: FormArray = soForm.get('MedicineGroup.MedicineArray');

for (let c of aFormArray.controls) {
    medObj.name.push(c.controls['MedName'].value);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角度反应形式在另一个 FormGroup 内的另一个 FormArray 内的嵌套 FormGroup 内添加 FormArray - angular reactive form add FormArray inside a nested FormGroup inside another FormArray inside another FormGroup 如何在Angular的FormArray反应形式中获得唯一值? - How to get unique value in FormArray reactive form in Angular? Angular Reactive Form中的FormArray不会在表单提交时重置 - FormArray in Angular Reactive Form not resetting on form submit 如何在 Angular 8 中正确使用表单数组/动态生成的表单与反应式表单 - How does one correctly use a formarray/dynamic generated form in conjunction with reactive forms in Angular 8 如何通过 Angular 9 中的 FormGroup (AbstractControl) 在 Reactive Form 中重置 ng-select? - How to reset ng-select in Reactive Form through a FormGroup (AbstractControl) in Angular 9? 具有反应形式和 formArray 的表中的角度列级过滤器 - Angular Column level filters in table having reactive form and formArray Angular 反应形式 - 复选框 - 使用现有数组初始化 formArray - Angular reactive form - checkboxes - Initialize formArray with exisitng array 没有formGroup如何控制formArray? - How to control formArray without formGroup? 如何以角度(反应形式)从父组件形式迭代数组 - How to iterate an array from parent componenent form in angular (reactive forms) Angular 反应式 Forms - FormArray 和 pacthValue - Angular Reactive Forms - FormArray and pacthValue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM