In this article, I am going to give you an Overview of Angular Forms. Please read our previous article where we discussed how to install bootstrap in angular application. At the end of this article, you will understand what are Angular Forms and their types and when when to use one over another.
Why we need Forms?
Forms are the main building blocks of any type of application. When we use forms for login, registration, submission. Help request, etc., it is necessary that whatever forms we are developing, they should be user friendly. And it should have the indication of what went wrong by display user friendly message, etc.
Forms are really very very important to collect the data from the users. Often, each website contains forms to collect the user data.
You can use forms to login, submit a help request, place an order, book a flight, schedule a meeting, and perform other countless data entry tasks.
What are Angular Forms?
Developing forms requires design skill as well as framework support for two-way data binding, change tracking, validation, error handling, etc. The Angular Framework, provides two different ways to collect and validate the data from a user. They are as follows:
- Template-Driven Forms
- Model-Driven Forms (Reactive Forms)
Template Driven Forms in Angular:
Template Driven Forms are simple forms which can be used to develop forms. These are called Template Driven as everything that we are going to use in an application is defined into the template that we are defining along with the component.
In order to use Template Driven Forms, we need to import FormsModule into the application root module i.e. app.module.ts file.
Features of Template Driven Forms:
- Easy to use.
- Suitable for simple scenarios and fail for complex scenarios.
- Similar to Angular 1.0 (Angular JS)
- Two way data binding using NgModule syntax.
- Minimal Component code
- Automatic track of the form and its data.
- Unit testing is another challenge
Model-Driven Forms (Reactive Forms) in Angular:
In a model driven approach, the model which is created in the .ts file is responsible for handling all the user interactions and validations. For this, first, we need to create the model using Angulars inbuilt classes like formGroup and formControl and then we need to bind that model to the HTML form.
This approach uses the Reactive forms for developing the forms which favor the explicit management of data between the UI (User Interface) and the Model. With this approach, we create the tree of Angular Form Controls and bind them in the Native Form Controls. As we create the form controls directly in the component, it makes it a bit easier to push the data between the data models and the UI elements.
In order to use Reactive Forms, you need to import ReactiveFormsModule into the applications root module i.e. app.module.ts file.
Features of Reactive Forms:
- More flexible, but need a lot of practice
- Handles any complex scenarios.
- No data binding is done (Immutable data model preferred by most developers).
- More component code and less HTML Markup.
- Easier unit testing.
- Reactive transformations can be made possible such as
- Handling a event based on a denounce time.
- Handling events when the components are distinct until changed.
- Adding elements dynamically.
Which one is better – Template Driven or Reactive Forms?
Neither reactive nor template driven are better over each other. For example, Template Driven forms are generally used to create simple forms. On the other hand, Reactive forms are used to create complex forms. For example, if you want to add form controls dynamically or perform cross-field validation, then you need to use the Reactive forms approach. There are several other differences, between Template driven and Reactive forms that we will discuss in our upcoming Videos.
Note: They both are two different approaches, so you can use whichever suits your needs the most. You can use both in the same application.