gpt4 book ai didi

Angular 10 FormArray ERROR 错误 : Cannot find control with name: '[object Object], [object Object],[object Object]

转载 作者:行者123 更新时间:2023-12-05 07:06:00 33 4
gpt4 key购买 nike

我正在尝试创建一个显示动态复选框的表单,至少应选中其中一个才能继续。我还需要获取一组选中的复选框。

这是组件的代码:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormControl, FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
selector: 'app-products',
templateUrl: './products.component.html',
styleUrls: ['./products.component.scss']
})
export class ProductsComponent implements OnInit {
productsForm: FormGroup;
items = [
{key: 'key1', text: 'text1'},
{key: 'key2', text: 'text2'},
{key: 'key3', text: 'text3'},
];
checkboxGroup = new FormArray(this.items.map(item => new FormGroup({
id: new FormControl(item.key),
text: new FormControl(item.text),
checkbox: new FormControl(false)
})));

constructor(private router: Router, private fb: FormBuilder) { }


ngOnInit() {

let hiddenControl = new FormControl(this.mapItems(this.checkboxGroup.value), Validators.required);

this.checkboxGroup.valueChanges.subscribe((v) => {
console.log(v);
hiddenControl.setValue(this.mapItems(v));
});

this.productsForm = new FormGroup({
items: this.checkboxGroup,
selectedItems: hiddenControl
});
console.log(this.checkboxGroup.controls);
}

mapItems(items) {
let selectedItems = items.filter((item) => item.checkbox).map((item) => item.id);
return selectedItems.length ? selectedItems : null;
}

onSubmit() {
this.router.navigate(['/result'], { skipLocationChange: true });
}
}

HTML:

<h3>Select products</h3>
<form [formGroup]="productsForm">
<div [formArrayName]="items" [class.invalid]="!productsForm.controls.selectedItems.valid">
<div *ngFor="let control of checkboxGroup.controls; let i = index;" [formGroup]="control">
<div>
<input type="checkbox" formControlName="checkbox" id="{{ control.controls.id.value }}">
<label attr.for="{{ control.controls.id.value }}">{{ control.controls.text.value }}</label>
</div>
</div>
</div>
<div [class.invalid]="!productsForm.controls.selectedItems.valid" *ngIf="!productsForm.controls.selectedItems.valid">
Check at least one checkbox!
</div>
<hr>
<pre>{{productsForm.controls.selectedItems.value | json}}</pre>
</form>

在 HTML 中,我以前尝试使用 FormGroup 实例循环访问 FormArray 实例,如下所示:productsForm.controls.items.controls。但它过去常常在编译期间返回错误,如下所示:

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

现在我摆脱了那个错误并且项目编译了,但现在它返回了

ERROR Error: Cannot find control with name: '[object Object],[object Object],[object Object]

这根本没有帮助。

如果我使用 console.log checkboxGroup.controls,它会按照我希望的方式返回我的复选框。

我正在使用 Angular 10

请帮忙!

最佳答案

不能称之为一个好的答案,就其本身而言,所以我仍然愿意接受更好的答案,但是这个会强制代码工作。

我回来使用 productsForm.controls.items.controls 进行迭代,但我还创建了一个如下所示的 getter:

 get checkboxControls() {
// assign type 'any' to this.productsForm.controls.items which previously was 'AbstractControls'
let checkbox = this.productsForm.controls.items as any
// now you can magically call the 'controls' property and iterate, congratulations
return checkbox.controls
}

在第 4 行的 HTML 中,将 checkboxGroup.controls 替换为我们新的 checkboxControls getter:

<div *ngFor="let control of checkboxControls; let i = index;" [formGroup]="control">
<div>
<input type="checkbox" formControlName="checkbox" id="{{ control.controls.id.value }}">
<label attr.for="{{ control.controls.id.value }}">{{ control.controls.text.value }}</label>
</div>
</div>

这是一个 demo如果有人想玩它,请链接。适用于 Angular 9 和 10。祝你好运!

关于Angular 10 FormArray ERROR 错误 : Cannot find control with name: '[object Object], [object Object],[object Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62623632/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com