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.