简体   繁体   English

Angular子窗体控件/组值和错误冒泡至父级

[英]Angular child form control/group values and errors bubbling up to parent

My requirement : A single FormControl must contain the answer of multiple inputs ( multiple child FormControls or FormGroups ). 我的要求 :单个FormControl必须包含多个输入的答案( 多个子FormControls或FormGroups )。

That is because multiple controls must feed into the top level (parent) FormControl, and the value of the parent level FormControl must be filtered/manipulated to fit the required format. 这是因为多个控件必须送入顶级(父)FormControl,并且父级FormControl的值必须经过过滤/操作以适合所需的格式。

A good example would be a split up phone input, with a Country code dropdown , phone number input , and extension input , all being separate from one another, but together in a FormGroup. 一个很好的例子是电话输入拆分,其中国家(地区)代码下拉列表电话号码输入分机输入 ,所有这些都彼此分开,但是一起在一个FormGroup中。 As any of those input values change, the parent level FormControl value must change as well. 随着任何这些输入值的更改,父级FormControl值也必须更改。

In addition to the values bubbling up, the errors of the child controls must bubble up to the parent FormControl too. 除了冒泡的值外,子控件的错误也必须冒泡到父FormControl。

Right now I have the parent FormControl listening to the value changes of a FormGroup with the country code, phone number, and extension inputs. 现在,我让父FormControl监听具有国家代码,电话号码和分机号输入的FormGroup的值更改。 Then, using ControlValueAccessor on a component, that writes the value of the parent FormControl when FormGroup ValueChanges. 然后,在组件上使用ControlValueAccessor,当FormGroup ValueChanges时写入父FormControl的值。

I need a way to achieve the desired outcome, but also a way to do even more complex controls and FormGroups. 我需要一种方法来实现所需的结果,但也需要一种方法来执行更复杂的控件和FormGroups。 Think multiple degrees of nested FormControls and/or FormGroups. 考虑多个级别的嵌套FormControls和/或FormGroups。

Environment 环境

  • Angular 7 角度7
  • Material 7 材料7
  • NGRX 6 NGRX 6

Initially trying to come up with a nice solution to handle breaking down huge forms into sub forms (sub components...), we came up with a solution for that but also to have access to nested errors. 最初,我们试图提供一个很好的解决方案来处理将巨大的表单分解为子表单(子组件...)的过程,我们想出了一个解决方案,但可以访问嵌套错误。

We've build a library for that https://github.com/cloudnc/ngx-sub-form that will give you way more than just access to nested errors and you can check the official demo here: https://cloudnc.github.io/ngx-sub-form/listings/new (edit the form and see the errors displayed) 我们已经为该https://github.com/cloudnc/ngx-sub-form构建了一个库,该库将为您提供更多的访问嵌套错误的方法,您可以在此处查看官方演示: https:// cloudnc。 github.io/ngx-sub-form/listings/new (编辑表单,查看显示的错误)

I've also replied to a similar SO question here https://stackoverflow.com/a/56375605/2398593 and built a demo for that question here (which also showcase the nested errors functionality) https://stackblitz.com/edit/so-question-angular-2-large-scale-application-forms-handling 我也在这里https://stackoverflow.com/a/56375605/2398593回答了一个类似的SO问题,并在这里为该问题构建了一个演示(也展示了嵌套错误功能) https://stackblitz.com/edit / SO-问题-角-2-大规模应用-表单的处理

Hope it helps! 希望能帮助到你!

Edit: 编辑:

If you want to go further, I've just published a blog post to explain a lot of things about forms and ngx-sub-form here https://dev.to/maxime1992/building-scalable-robust-and-type-safe-forms-with-angular-3nf9 如果您想走得更远,我刚刚在这里发布了一篇博客文章,以解释有关表单和ngx-sub-form的许多内容,网址为https://dev.to/maxime1992/building-scalable-robust-and-type-安全表单与-角3nf9

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM