Angular Provides two types ways for handling user input through forms: reactive forms and template driven forms. So in this tutorial, we'll explore reactive and template driven-forms in Angular.
Template-driven forms
Template-driven approach is used when we need to develop static forms, Means the structure and logic of the form is fixed, Like Registration form, Login Form, Reset password form, add order form.
Pros
- Template-driven forms are easier to create.
- Suitable for simple scenarios
- Less component code
- form structure and logic is mainly implemented in HTML
- uses pure web standards, like required is used to make the input field mandatory.
- One-way and two-way data-binding.
- Similar to Angular 1.
Cons
- Unit testing is a challenge here.
- limited capabilities to implement dynamic aspects like the variable number of fields, repetitive fields, etc.
- Not good for the application with complex forms.
- It's not a good option anyway when we have Reactive form available.
Reactive forms
The structure and logic Reactive form are mainly written in Typescript file. It can easily handle the data when we have dynamic fields, repetitive fields in our form. The exchange of data from HTML to TypeScript is performed by passing the form’s value property from HTML to TypeScript.
Pros
- Form definition and logic is mainly implemented in TypeScript
- Provides full control of form value updates and form validation.
- Suitable for creation of forms with dynamic structure at runtime
- Supports implementation of custom form validation
- most of the interaction occurs in the component file.
- Handles any complex scenarios
- Easier unit testing
Cons
- Requires more coding, Mainly in TypeScript file.
- More flexible, but needs a lot of practice.
- More difficult to understand and to maintain
Conclusion
In this article, We tried to understand the Difference between Reactive forms and Template driven forms in Angular in brief. If you are new in the world of Angular then you can find so many Angular Sample Application.
Let me know your thoughts over the email demo.jsonworld@gmail.com. I would love to hear them and If you like this article, share it with your friends.