angular - 角度反应形式:将子组件值传递给父组件
问题描述
我以角度实现反应形式。我无法将价值从孩子传递给父母。
父表单示例:
<div class="fields-wrap" [formGroup]="parent">
<div class="input-wrap">
<child-component></child-component>
</div>
<input type"button">Submit</div>
</div>
子组件形式:
<div class="fields-wrap" [formGroup]="child">
<input type="text" formControlName="firstname" />
<input type="text" formControlName="lastname" />
<input type="text" formControlName="email" />
</div>
当我从父组件单击提交时如何获取这些子值。谁能帮我?
解决方案
Form
而不是作为@Input()
你可以使用的传递viewProviders
,我发现它阅读起来更干净。
要做到这一点非常简单,在您的子组件打字稿文件中,您只需向您的@Component
viewProviders: [ { provide: ControlContainer, useExisting: FormGroupDirective }]
所以总的来说它会输出如下:
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss'],
viewProviders: [ { provide: ControlContainer, useExisting: FormGroupDirective }]
})
stackblitz 示例:https ://stackblitz.com/edit/angular-ruxfee
推荐阅读
- python - 如何将频谱图图像与其人工标记的数据结合起来,在 Python 中使用 CNN 进行处理?
- ios - 一个字符串具有多种段落样式
- javascript - 是否有任何在时间更改时执行的 PrimeNg p-calendar 事件?
- javascript - 跨多个 Ajax POST 调用在成功函数中存储和更新全局值
- c# - 如何使用 c# API 授权 Azure Devops?
- hadoop - 如何在直线开始时删除 ADD jar 语句
- android - 如何更改 android compose TextFeild 的内容填充?
- java - 在 AsyncTask 中更新后自动/立即更新进度条
- postgresql - 来自 pg_range 和 pg_type 的查询不断占用我服务器的资源 - postgresql
- node.js - 将 cURL 命令转换为 JavaScript