Best practice to handle large form in Angular 2 [c

2019-09-02 21:06发布

问题:

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.