GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing angular.dev — Forms • Overview • Angular Applications use forms to enable users to log in, to update a profile, to enter sensitive information, and to perform many other data-entry tasks. Angular provides two different approaches to handling user input through forms: reactive and template-driven. Both capture user input events from the view, validate the input, create a form and data model, and provide a way to track changes. TIP: If you're looking for the new Signal Forms, check out our essential Signal Forms guide! This guide provides information to help you decide which type of form works best for your situation. It introduces the common building blocks used by both approaches. It also summarizes the key differences between the two approaches, and demonstrates those differences in the context of setup, data flow, and testing. Choosing an approachReactive forms and template-driven forms process and manage form data differently. Each approach offers different advantages.
Key differencesThe following table summarizes the key differences between reactive and template-driven forms.
ScalabilityIf forms are a central part of your application, scalability is very important. Being able to reuse form models across components is critical. Reactive forms are more scalable than template-driven forms. They provide direct access to the underlying form API, and use synchronous data flow between the view and the data model, which makes creating large-scale forms easier. Reactive forms require less setup for testing, and testing does not require deep understanding of change detection to properly test form updates and validation. Template-driven forms focus on simple scenarios and are not as reusable. They abstract away the underlying form API, and use asynchronous data flow between the view and the data model. The abstraction of template-driven forms also affects testing. Tests are deeply reliant on manual change detection execution to run properly, and require more setup. Setting up the form modelBoth reactive and template-driven forms track value changes between the form input elements that users interact with and the form data in your component model. The two approaches share underlying building blocks, but differ in how you create and manage the common form-control instances. Common form foundation classesBoth reactive and template-driven forms are built on the following base classes.
Setup in reactive formsWith reactive forms, you define the form model directly in the component class.
The The following component implements an input field for a single control, using reactive forms.
In this example, the form model is the
IMPORTANT: In reactive forms, the form model is the source of truth; it provides the value and status of the form element at any given point in time, through the Setup in template-driven formsIn template-driven forms, the form model is implicit, rather than explicit.
The directive The following component implements the same input field for a single control, using template-driven forms.
IMPORTANT: In a template-driven form, the source of truth is the template. The Data flow in formsWhen an application contains a form, Angular must keep the view in sync with the component model and the component model in sync with the view. As users change values and make selections through the view, the new values must be reflected in the data model. Similarly, when the program logic changes values in the data model, those values must be reflected in the view. Reactive and template-driven forms differ in how they handle data flowing from the user or from programmatic changes. The following diagrams illustrate both kinds of data flow for each type of form, using the favorite-color input field defined above. Data flow in reactive formsIn reactive forms, each form element in the view is directly linked to the form model (a The view-to-model diagram shows how data flows when an input field's value is changed from the view through the following steps:
The model-to-view diagram shows how a programmatic change to the model is propagated to the view through the following steps.
Data flow in template-driven formsIn template-driven forms, each form element is linked to a directive that manages the form model internally. The view-to-model diagram shows how data flows when an input field's value is changed from the view through the following steps.
The model-to-view diagram shows how data flows from model to view when the
NOTE: Mutability of the data modelThe change-tracking method plays a role in the efficiency of your application.
The difference is demonstrated in the previous examples that use the favorite-color input element.
Form validationValidation is an integral part of managing any set of forms. Whether you're checking for required fields or querying an external API for an existing username, Angular provides a set of built-in validators as well as the ability to create custom validators.
For more information, see Form Validation. TestingTesting plays a large part in complex applications. A simpler testing strategy is useful when validating that your forms function correctly. Reactive forms and template-driven forms have different levels of reliance on rendering the UI to perform assertions based on form control and form field changes. The following examples demonstrate the process of testing forms with reactive and template-driven forms. Testing reactive formsReactive forms provide a relatively straightforward testing strategy because they provide synchronous access to the form and data models, and they can be tested without rendering the UI. In these tests, status and data are queried and manipulated through the control without interacting with the change detection cycle. The following tests use the favorite-color components from previous examples to verify the view-to-model and model-to-view data flows for a reactive form. Verifying view-to-model data flowThe first example performs the following steps to verify the view-to-model data flow.
Favorite color test - view to model
The next example performs the following steps to verify the model-to-view data flow.
Favorite color test - model to view
Testing template-driven formsWriting tests with template-driven forms requires a detailed knowledge of the change detection process and an understanding of how directives run on each cycle to ensure that elements are queried, tested, or changed at the correct time. The following tests use the favorite color components mentioned earlier to verify the data flows from view to model and model to view for a template-driven form. The following test verifies the data flow from view to model. Favorite color test - view to model
Here are the steps performed in the view to model test.
The following test verifies the data flow from model to view. Favorite color test - model to view
Here are the steps performed in the model to view test.
Next stepsTo learn more about reactive forms, see the following guides: To learn more about template-driven forms, see the following guides: | |||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |