21 Using the forms API, part 1

 

This chapter covers

  • Using the Angular API to create and manage form elements
  • Validating form elements created with the API
  • Grouping form elements programmatically

In this chapter, I describe the Angular forms API, which provides an alternative to the template-based approach to forms introduced in chapter 13. The forms API is a more complicated way of creating forms, but it allows fine-grained control over how forms behave, how they respond to user interaction, and how they are validated. Table 21.1 puts the forms API in context.

Table 21.1. Putting the forms API in context
Question
Answer
What is it?
The forms API allows for the creation of reactive forms, which are managed using the code in a component class.
Why is it useful?
The forms API provides a component with more control over the elements in forms and allows their behavior to be customized.
How is it used?
FormControl and FormGroup objects are created by the component class and associated with elements in the template using directives.
Are there any pitfalls or limitations?
The forms API is complex, and additional work is required to ensure that features such as validation behave consistently.
Are there any alternatives?
The forms API is optional. Forms can be defined using the basic features described in chapter 13.

Table 21.2 summarizes the chapter.

21.1 Preparing for this chapter

21.2 Understanding the reactive forms API

21.3 Rebuilding the form using the API

21.3.1 Responding to form control changes

21.3.2 Managing control state

21.3.3 Managing control validation

21.3.4 Adding additional controls

21.4 Working with multiple form controls

21.4.1 Using a form group with a form element

21.4.2 Accessing the form group from the template

21.4.3 Displaying validation messages with a form group

21.5 Summary