Angular Forms

Angular Forms

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:

  1. Template-Driven Forms
  2. 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:
  1. Easy to use.
  2. Suitable for simple scenarios and fail for complex scenarios.
  3. Similar to Angular 1.0 (Angular JS)
  4. Two way data binding using NgModule syntax.
  5. Minimal Component code
  6. Automatic track of the form and its data.
  7. 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 Angular’s 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:
  1. More flexible, but need a lot of practice
  2. Handles any complex scenarios.
  3. No data binding is done (Immutable data model preferred by most developers).
  4. More component code and less HTML Markup.
  5. Easier unit testing.
  6. Reactive transformations can be made possible such as
  7. Handling a event based on a denounce time.
  8. Handling events when the components are distinct until changed.
  9. 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 articles.

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.

In the next article, I am going to discuss Template Driven Forms in Angular with examples. Here, in this article, I try to explain what are Angular Forms and their types and when to use Template Driven Forms over Reactive Forms and vice-verse. I hope you enjoy this article.

3 thoughts on “Angular Forms”

Leave a Reply

Your email address will not be published. Required fields are marked *