angular - Angular FormGroupArray 元素传递给组件
问题描述
我有一个角度形式,其中包含我想传递给子组件的表单组数组。
this.myForm = this.fb.group({
name: [''],
email: [''],
installations: this.fb.array([]),
...
我的初始化程序创建数组中的第一项:
get installations(){
return this.myForm.get('installations') as FormArray;
}
addInstallation(){
this.installations.push(
this.fb.group({
item1: [''],
item2: ['']
}
我在我的 html 中迭代安装
<mat-expansion-panel *ngFor="let inst of installations.controls; let i = index">
<mat-expansion-panel-header>
<mat-panel-title>Installation {{ i + 1 }}</mat-panel-title>
</mat-expansion-panel-header>
<app-installation-details [formGroupName]="i"></app-installation-details>
</mat-expansion-panel>
所以我希望我的 app-installation-details 子组件能够单独安装:
public installationDetailsFormGroup: FormGroup;
constructor(private controlContainer: ControlContainer, private fb: FormBuilder) { }
ngOnInit(): void {
this.installationDetailsFormGroup = <FormGroup>this.controlContainer.control;
}
但控制台总是抱怨:
Cannot find control with name: '0'
如果我更改通过的项目,它会说同样的话:
<app-installation-details [formGroupName]="inst"></app-installation-details>
错误与
Cannot find control with name: '[object Object]'
解决方案
推荐阅读
- javascript - Angular Universal 会在短时间内加载登录页面
- sockets - 收听传入的whatsapp消息
- sql - SQL Server 代理作业运行成功但不输出数据
- java - 给变量赋值
- javascript - 我使用 JS 制作了秒表,但它不起作用
- python-3.x - Nginx 无法使用在 AWS EC2 实例上运行的 Flask 应用程序从登录重定向到主页
- android - 有没有办法为 Android APP 配置 DoH(DNS over HTTPS)或 DoT(DNS over TLS)?
- python-3.x - 基于空列分隔符将 Pandas 数据帧拆分为多个数据帧
- python - 如何为每次迭代打印来自for循环的while循环的输出
- selenium - 修复从 Cucumber 上的特征文件创建步骤定义时的“步骤定义目录路径不正确”错误