There is a large form in my application. What is the best practice to retrieve data from that form and send to the API in Angular 5 ?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Best practices are :
- Separate your form in a component
- Using ngForm to build your form
That's pretty easy
回答2:
I would use a reactive dynamic form builder like the examples here.
https://angular.io/guide/dynamic-form
https://stackblitz.com/angular/relgkgnvgxp
https://toddmotto.com/angular-dynamic-components-forms
As far as submission you can either fire onChange
or with an onSubmit
function attached to the <form>
element. Then simply add a button of type="submit"
within the form.
This will give you the most robust usage of angular validation. I would also recommend exporting the form into a stand alone component. You can even use an event emitter to pass the form values to the parent component and handle the actual post request there for more reusability.