Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactiveFormsModule vs. FormsModule in Angular2

There exists the ReactiveFormModule and the FormsModule.

import {
  FormsModule,
  ReactiveFormsModule
} from "@angular/forms";

When should I use the ReactiveFormModule and what provides this module comparing to the FormModule.

like image 882
muetzerich Avatar asked Sep 22 '16 11:09

muetzerich


2 Answers

Formsmodule - Angular 1 tackles forms via the famous ng-model directive. Angular 2 now provides an identical mechanism named also ngModel, that allow us to build what is now called Template-Driven forms.

Unlike the case of AngularJs, ngModel and other form-related directives are not available by default.

ReactiveFormsModule is for model driven forms. Each form has a state that can be updated by many different interactions and its up to the application developer to manage that state and prevent it from getting corrupted. This can get hard to do for very large forms and can introduce a category of potential bugs.

Where you can write Validations not in html. In this case you can attach many Validations and wrap form.

I got this from here. There is better explanation:

like image 127
oto lolua Avatar answered Oct 14 '22 01:10

oto lolua


I would like to clarify Oto Lolua's answer. Their explanation for the ReactiveFormsModule was taken out from the wrong context in the article they linked (Oto's explanatory paragraph for the ReactiveFormsModule was actually in reference to FormsModule, not ReactiveFormsModule, in that article).

I'm quoting that same article here (from its summary):

Template Driven Forms are maybe for simple forms slightly less verbose, but the difference is not significant. Reactive Forms are actually much more powerful and have a nearly equivalent readability.

Most likely in a large scale application we will end up needing the functionality of reactive driven forms for implementing more advanced use cases like for example auto-save.

like image 27
pbre Avatar answered Oct 14 '22 00:10

pbre