This chapter will cover the following recipes:
Forms are important elemental constructs for nearly every web application, and they have been reimagined for the better in Angular 2. Angular 1 forms were very useful, but they were totally dependent on the conventions of ngModel
. Angular 2's newfound conventions remove it from ngModel
dependence and offer a fresh approach to form and information management that ultimately feels cleaner and more approachable.
Fundamentally, it is important to understand where and why forms are useful. There are many places in an application where multitudinous input demands association, and forms are certainly useful in this context. Angular 2 forms are best used when validating the said input, especially so when multiple-field and cross-field validation is required. Additionally, Angular forms maintain the state of various form elements, allowing the user to reason the "history" of an input field.
It is also critical to remember that the Angular 2 form behavior, much in the same way as its event and data binding, is getting integrated with the already robust browser form behavior. Browsers are already very capable of submitting data, recalling data upon a page reload, simple validation, and other behaviors that pretty much all forms rely upon. Angular 2 doesn't redefine these; rather, it integrates with these behaviors in order to link in other behaviors and data that are part of either a framework or your application.
In this chapter, be aware of the duality of the use of FormsModule
and ReactiveFormsModule
. They behave very differently and are almost always used separately when it comes to form construction.