angular - 带有父子路由的反应式表单
问题描述
我有一个数据输入表单,我正在使用父/子路由器来定义一个呈现一些选项卡的父组件,每个选项卡加载正在编辑的实体的一部分。
例如:我有一个渲染一些选项卡的组件,每个选项卡路由到一个子路由并渲染模型的一部分(请参见下面的路由)
[
{
path: ':id',
component: ParentComponent,
resolve: {someModel: SomeModelResolver },
children: [
{ path: '', redirectTo: 'summary', pathMatch: 'full' },
{ path: 'summary', component: SummaryComponent },
{ path: 'other', component: OtherComponent },
...
]
}
]
我在所有子组件中使用反应式表单,但遇到了一些麻烦
a)访问所有子路由的数据(我想我可以通过activatedRoute.parent.data来做到这一点),并且;
b)验证所有子路由的数据,因为保存/取消按钮位于父路由组件中
请问有人对我如何实现这一点有任何建议吗?(我更喜欢反应式表单解决方案而不是模板驱动的表单)
解决方案
Another approach which works really well without using router but DI by passing child formGroups via @Input decorators to the child components. Tested this with really huge forms using @angular/material‘s mat-accordion and mat-tab-group. Performance is quite good and the form gets nicely separated into smaller peaces like you want it to.
Create getter functions for these child formGroups
get childform1() {
return this.form.get(‘childForm1‘) as FormGroup;
}
and then just use
<app-childform [form]=“childform1“></app-childform>
In your child
@Input() form: FormGroup;
Then you can save your complete form easily on your parent component and all values will get passed into it nicely.
推荐阅读
- ios - 返回一个对象“没有更多上下文的表达式类型不明确”
- amazon-cloudformation - AWS CDK - 如何将“占位符”令牌与低级 cfn 构造一起使用
- javascript - 是否可以防止计时器结束?
- python - lxml如何在使用openpyxl时加快保存大型excel文件的速度?
- mysql - GROUP_CONCAT 不考虑过滤器
- rust - 如何固定投影矢量的元素?
- javascript - 无法将电子相关文件导入 Vue 3
- javascript - 如何在 Javascript 中删除视频效果?
- html - 我无法将我的 CSS 文件链接到我的 HTML 文件
- pandas - 在python中sns relplot的facetgrid中注释文本