diff --git a/aio/content/examples/forms-overview/e2e/src/app.e2e-spec.ts b/aio/content/examples/forms-overview/e2e/src/app.e2e-spec.ts new file mode 100644 index 0000000000000..b15faa2a4c8a5 --- /dev/null +++ b/aio/content/examples/forms-overview/e2e/src/app.e2e-spec.ts @@ -0,0 +1,10 @@ +import { browser, element, by } from 'protractor'; + +describe('Forms Overview Tests', function () { + + beforeEach(function () { + browser.get(''); + }); + +}); + diff --git a/aio/content/examples/forms-overview/example-config.json b/aio/content/examples/forms-overview/example-config.json new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/aio/content/examples/forms-overview/src/app/app.component.css b/aio/content/examples/forms-overview/src/app/app.component.css new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/aio/content/examples/forms-overview/src/app/app.component.html b/aio/content/examples/forms-overview/src/app/app.component.html new file mode 100644 index 0000000000000..ab1a839f59703 --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/app.component.html @@ -0,0 +1,10 @@ + +

Forms Overview

+ +

Reactive

+ + + +

Template-Driven

+ + \ No newline at end of file diff --git a/aio/content/examples/forms-overview/src/app/app.component.spec.ts b/aio/content/examples/forms-overview/src/app/app.component.spec.ts new file mode 100644 index 0000000000000..1ab69b01846bc --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/app.component.spec.ts @@ -0,0 +1,31 @@ +import { TestBed, async } from '@angular/core/testing'; +import { AppComponent } from './app.component'; +import { TemplateModule } from './template/template.module'; +import { ReactiveModule } from './reactive/reactive.module'; + +describe('AppComponent', () => { + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [ReactiveModule, TemplateModule], + declarations: [ + AppComponent + ], + }).compileComponents(); + })); + + it('should create the app', async(() => { + const fixture = TestBed.createComponent(AppComponent); + const app = fixture.debugElement.componentInstance; + + expect(app).toBeTruthy(); + })); + + it('should render title in a h1 tag', async(() => { + const fixture = TestBed.createComponent(AppComponent); + fixture.detectChanges(); + + const compiled = fixture.debugElement.nativeElement; + expect(compiled.querySelector('h1').textContent).toContain('Forms Overview'); + })); +}); diff --git a/aio/content/examples/forms-overview/src/app/app.component.ts b/aio/content/examples/forms-overview/src/app/app.component.ts new file mode 100644 index 0000000000000..26f42471ce994 --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/app.component.ts @@ -0,0 +1,10 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.css'] +}) +export class AppComponent { + title = 'forms-intro'; +} diff --git a/aio/content/examples/forms-overview/src/app/app.module.ts b/aio/content/examples/forms-overview/src/app/app.module.ts new file mode 100644 index 0000000000000..1991c7ae17b56 --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/app.module.ts @@ -0,0 +1,20 @@ +import { BrowserModule } from '@angular/platform-browser'; +import { NgModule } from '@angular/core'; + +import { AppComponent } from './app.component'; +import { ReactiveModule } from './reactive/reactive.module'; +import { TemplateModule } from './template/template.module'; + +@NgModule({ + declarations: [ + AppComponent, + ], + imports: [ + BrowserModule, + ReactiveModule, + TemplateModule + ], + providers: [], + bootstrap: [AppComponent] +}) +export class AppModule { } diff --git a/aio/content/examples/forms-overview/src/app/reactive/name/name.component.spec.ts b/aio/content/examples/forms-overview/src/app/reactive/name/name.component.spec.ts new file mode 100644 index 0000000000000..8b63989191877 --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/reactive/name/name.component.spec.ts @@ -0,0 +1,62 @@ +// #docregion +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { ReactiveFormsModule } from '@angular/forms'; + +import { ReactiveNameComponent } from './name.component'; +import { createNewEvent } from '../../shared/utils'; + +// #docregion tests +describe('Reactive Name Component', () => { +// #enddocregion tests + let component: ReactiveNameComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [ReactiveFormsModule], + declarations: [ReactiveNameComponent] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ReactiveNameComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + +// #docregion tests + it('should update the value in the control', () => { + // update the control value + component.name.setValue('Nancy'); + + // query the element + const input = fixture.nativeElement.querySelector('input'); + + // check its value + expect(input.value).toBe('Nancy'); + }); + + it('should update the value of the input field', () => { + // update the control value + component.name.setValue('Nancy'); + + // query the element + const input = fixture.nativeElement.querySelector('input'); + expect(input.value).toEqual('Nancy'); + + // update the form field value + input.value = 'Smith'; + + // Use utility function to create custom event, then dispatch on the input + const event = createNewEvent('input'); + input.dispatchEvent(event); + + expect(fixture.componentInstance.name.value).toEqual('Smith'); + }); +}); +// #enddocregion diff --git a/aio/content/examples/forms-overview/src/app/reactive/name/name.component.ts b/aio/content/examples/forms-overview/src/app/reactive/name/name.component.ts new file mode 100644 index 0000000000000..00b6f0bfd0b0e --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/reactive/name/name.component.ts @@ -0,0 +1,17 @@ +// #docplaster +// #docregion +import { Component } from '@angular/core'; +import { FormControl } from '@angular/forms'; + +@Component({ +// #enddocregion + selector: 'app-reactive-name', +// #docregion + template: ` + Name: + ` +}) +export class ReactiveNameComponent { + name = new FormControl(''); +} +// #enddocregion diff --git a/aio/content/examples/forms-overview/src/app/reactive/reactive.module.spec.ts b/aio/content/examples/forms-overview/src/app/reactive/reactive.module.spec.ts new file mode 100644 index 0000000000000..3add422b837f8 --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/reactive/reactive.module.spec.ts @@ -0,0 +1,13 @@ +import { ReactiveModule } from './reactive.module'; + +describe('ReactiveModule', () => { + let reactiveModule: ReactiveModule; + + beforeEach(() => { + reactiveModule = new ReactiveModule(); + }); + + it('should create an instance', () => { + expect(reactiveModule).toBeTruthy(); + }); +}); diff --git a/aio/content/examples/forms-overview/src/app/reactive/reactive.module.ts b/aio/content/examples/forms-overview/src/app/reactive/reactive.module.ts new file mode 100644 index 0000000000000..c02a4e6b7b859 --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/reactive/reactive.module.ts @@ -0,0 +1,14 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { ReactiveNameComponent } from './name/name.component'; +import { ReactiveFormsModule } from '@angular/forms'; + +@NgModule({ + imports: [ + CommonModule, + ReactiveFormsModule + ], + declarations: [ReactiveNameComponent], + exports: [ReactiveNameComponent], +}) +export class ReactiveModule { } diff --git a/aio/content/examples/forms-overview/src/app/shared/utils.ts b/aio/content/examples/forms-overview/src/app/shared/utils.ts new file mode 100644 index 0000000000000..a7cb3ce69ca42 --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/shared/utils.ts @@ -0,0 +1,5 @@ +export function createNewEvent(eventName: string, bubbles = false, cancelable = false) { + let evt = document.createEvent('CustomEvent'); + evt.initCustomEvent(eventName, bubbles, cancelable, null); + return evt; +} diff --git a/aio/content/examples/forms-overview/src/app/template/name/name.component.spec.ts b/aio/content/examples/forms-overview/src/app/template/name/name.component.spec.ts new file mode 100644 index 0000000000000..63ad3dfdedf05 --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/template/name/name.component.spec.ts @@ -0,0 +1,76 @@ +// #docregion +import { async, ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing'; +import { FormsModule } from '@angular/forms'; + +import { createNewEvent } from '../../shared/utils'; +import { TemplateNameComponent } from './name.component'; + +// #docregion tests +describe('Template Name Component', () => { +// #enddocregion tests + let component: TemplateNameComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [FormsModule], + declarations: [TemplateNameComponent] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(TemplateNameComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + +// #docregion tests + it('should update the value in the control', fakeAsync(() => { + // update the component instance variable + component.name = 'Nancy'; + + // run change detection + fixture.detectChanges(); + + // advance after change detection cycle + tick(); + + // query the element + const input = fixture.nativeElement.querySelector('input'); + + expect(input.value).toBe('Nancy'); + })); + + it('should update the value of the input field', fakeAsync(() => { + // update component instance variable + component.name = 'Nancy'; + + // run change detection + fixture.detectChanges(); + + // advance after change detection cycle + tick(); + + // query the element + const input = fixture.nativeElement.querySelector('input'); + expect(input.value).toEqual('Nancy'); + + // update the form field value + input.value = 'Smith'; + + // Use utility function to create custom event, then dispatch on the input + const event = createNewEvent('input'); + input.dispatchEvent(event); + + // advance after change detection cycle + tick(); + + expect(component.name).toEqual('Smith'); + })); +}); +// #enddocregion diff --git a/aio/content/examples/forms-overview/src/app/template/name/name.component.ts b/aio/content/examples/forms-overview/src/app/template/name/name.component.ts new file mode 100644 index 0000000000000..461032c46bc58 --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/template/name/name.component.ts @@ -0,0 +1,16 @@ +// #docplaster +// #docregion +import { Component } from '@angular/core'; + +@Component({ +// #enddocregion + selector: 'app-template-name', +// #docregion + template: ` + Name: + ` +}) +export class TemplateNameComponent { + name = ''; +} +// #enddocregion diff --git a/aio/content/examples/forms-overview/src/app/template/template.module.spec.ts b/aio/content/examples/forms-overview/src/app/template/template.module.spec.ts new file mode 100644 index 0000000000000..cb28c36acd91e --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/template/template.module.spec.ts @@ -0,0 +1,13 @@ +import { TemplateModule } from './template.module'; + +describe('TemplateModule', () => { + let templateModule: TemplateModule; + + beforeEach(() => { + templateModule = new TemplateModule(); + }); + + it('should create an instance', () => { + expect(templateModule).toBeTruthy(); + }); +}); diff --git a/aio/content/examples/forms-overview/src/app/template/template.module.ts b/aio/content/examples/forms-overview/src/app/template/template.module.ts new file mode 100644 index 0000000000000..c154b4962ba4e --- /dev/null +++ b/aio/content/examples/forms-overview/src/app/template/template.module.ts @@ -0,0 +1,14 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { TemplateNameComponent } from './name/name.component'; +import { FormsModule } from '@angular/forms'; + +@NgModule({ + imports: [ + CommonModule, + FormsModule + ], + declarations: [TemplateNameComponent], + exports: [TemplateNameComponent] +}) +export class TemplateModule { } diff --git a/aio/content/examples/forms-overview/src/index.html b/aio/content/examples/forms-overview/src/index.html new file mode 100644 index 0000000000000..a4dc0c2ba3a09 --- /dev/null +++ b/aio/content/examples/forms-overview/src/index.html @@ -0,0 +1,14 @@ + + + + + Forms Overview + + + + + + + + + diff --git a/aio/content/examples/forms-overview/src/main.ts b/aio/content/examples/forms-overview/src/main.ts new file mode 100644 index 0000000000000..91ec6da5f0788 --- /dev/null +++ b/aio/content/examples/forms-overview/src/main.ts @@ -0,0 +1,12 @@ +import { enableProdMode } from '@angular/core'; +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + +import { AppModule } from './app/app.module'; +import { environment } from './environments/environment'; + +if (environment.production) { + enableProdMode(); +} + +platformBrowserDynamic().bootstrapModule(AppModule) + .catch(err => console.log(err)); diff --git a/aio/content/examples/forms-overview/stackblitz.json b/aio/content/examples/forms-overview/stackblitz.json new file mode 100644 index 0000000000000..b3eefdff14b07 --- /dev/null +++ b/aio/content/examples/forms-overview/stackblitz.json @@ -0,0 +1,7 @@ +{ + "description": "Forms Overview", + "files":[ + "!**/*.d.ts", + "!**/*.js" + ] +} diff --git a/aio/content/guide/forms-overview.md b/aio/content/guide/forms-overview.md index 154d933b17dbb..a90aec5822b32 100644 --- a/aio/content/guide/forms-overview.md +++ b/aio/content/guide/forms-overview.md @@ -4,81 +4,6 @@ Handling user input with forms is the cornerstone of many common applications. Y Angular provides two different approaches to handling user input through forms: reactive and template-driven. Each set of forms promote a distinct way of processing forms and offers different advantages. The sections below provide a comparison of the two approaches and when each one is applicable. There are many different factors that influence your decision on which approach works best for your situation. Whether you’re using reactive or template-driven forms, these concepts are key to understanding the mechanisms underneath each solution. -In general: - -* **Reactive forms** are more robust: they are more scalable, reusable, and testable. If forms are a key part of your application, or you're already using reactive patterns for building your application, use reactive forms. -* **Template-driven forms** are useful for adding a simple form to an app, such as a single email list signup form. They are easy to add to an app, but they do not scale as well as reactive forms. If you have very basic form requirements and logic that can be managed solely in the template, use template-driven forms. - -## A common foundation - -Both reactive and template-driven forms share underlying building blocks, the `FormControl`, `FormGroup` and `FormArray`. A `FormControl` instance tracks the value and validation status of an individual form control element, a `FormGroup` instance tracks the same values and statuses for a collection, and a `FormArray` instance tracks the same values and statues for an array of form controls. How these control instances are created and managed with reactive and template-driven forms will be discussed later in this guide. - -## Control value accessors - -Control value accessors define a bridge between Angular forms and native DOM elements. The `ControlValueAccessor` interface defines methods for interacting with native elements including: reading from and writing values to them, disabling or enabling their elements, and providing callback functions for when the control's value changes in the UI or becomes touched. A built-in accessor is attached to every form field when using either forms module in Angular, unless a custom value accessor has been activated on that field. - -## Data flow in forms - -When building forms in Angular, it's important to understand how the the framework handles data flowing from the user or from programmatic changes. Reactive and template-driven follow two different strategies when handling these scenarios. Using a simple component with a single input field, we can illustrate how changes are handled. - -### Data flow in reactive forms - -Here is a component with an input field for a single control using reactive forms. - -```typescript -@Component({ - template: `Name: ` -}) -export class ReactiveNameComponent { - name = new FormControl(''); -} -``` - -Let’s look at how the data flows for an input with reactive forms. - -**Diagram of Input Event Flow For Reactive Forms** - -### Data flow in template-driven forms - -In reactive forms, the source of truth is the form model (in this case, the `FormControl` instance), which is explicitly defined in the component class. This model is created independently of the UI and can be used to provide an initial value for the control. The reactive form directive (in this case, `FormControlDirective`) then links the existing form control instance to a specific form element in the view using a value accessor. - -When text is entered into the input field, the field's value accessor immediately relays the new value to the FormControl instance, which then emits the value through the valueChanges observable. - -With reactive forms, you have full control over the form model without ever rendering the UI. The source of truth is always up-to-date, because it is synchronously updated at the time changes are made. - -Now let’s look at the same data flows with template-driven forms. - -```typescript -@Component({ - template: `Name: ` -}) -export class TemplateNameComponent { - name = ''; -} -``` - -**Diagram of Input Event Flow For Reactive Forms** - -In template-driven forms, the source of truth is the template, so developers create their desired form through the placement of template-driven directives such as `NgModel` and `NgModelGroup`. The directives then create the `FormControl` or `FormGroup` instances that make up the form model, link them with form elements through a value accessor, and manage them within the constraints of the template's change detection cycle. - -This abstraction promotes simplicity over structure. It is less explicit, but you no longer have direct control over the form model. It is simple to add directives, but because these directives are dependent on the UI, they must work around the change detection cycle. Programmatic value changes are registered during change detection (as they occur through an `Input` to a directive), so it's not possible to update the value and validity immediately because it may affect elements that have already been checked in that view, for example, the parent form. For this reason, value changes are delayed until the next tick, which allows change detection to complete and a new change detection process to be triggered with the updated value. In other words, the process happens asynchronously and introduces unpredictability for querying the source of truth. - -## Custom validation and testing - -Validation 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. With reactive forms, custom validators are functions that receive a control to validate. Because template-driven forms are tied to directives, custom validator directives must be created to wrap a validator function in order to use it in a template. - -For more on form validation, visit the [Form Validation](guide/form-validation) guide. - -Testing also plays a large part in complex applications and an easier testing strategy is always welcomed. Reactive forms provide an easy testing strategy to due to synchronous access to the form and data models, where controls and data can be queried and manipulated easily through the control without rendering a view. Template-driven forms are asynchronous, which complicates complex testing scenarios. It involves more detailed knowledge of the change detection process and how directives run on each cycle to ensure elements can be queried at the correct time, you must wait for the appropiate lifecycle hooks to finish before extracting any values, testing its validity or changing its value. - -## Mutability - -How changes are tracked plays a role in the efficiency of your application. Reactive forms keep the data model pure by providing it as an immutable data structure. Each time the a change is triggered on the data model, a new data model is returned rather than updating the data model directly. This is more efficient, giving you the ability track unique changes to the data model. It also follows reactive patterns that integrate with observable operators to map and transform data. Template-driven forms rely on mutability with two-way data binding to update the data model in the component as changes are made in the template. - -## Scalability - -If forms are a central part of your application, scalability is very important. Being able to reuse form models across components and data access is critical. Reactive forms makes creating large scale forms easier by providing access to low-level APIs and synchronous access to the data model. Because template-driven forms focus on simplicity and simple scenarios with static content and little validation, they are not as reusable and abstract away the low-level APIs and access to the data model is handled asynchronously. - ## Key differences The table below summarizes the key differences in reactive and template driven forms. @@ -87,7 +12,7 @@ The table below summarizes the key differences in reactive and template driven f td, th {vertical-align: top} - +
@@ -235,6 +160,91 @@ The table below summarizes the key differences in reactive and template driven f
+In general: + +* **Reactive forms** are more robust: they are more scalable, reusable, and testable. If forms are a key part of your application, or you're already using reactive patterns for building your application, use reactive forms. +* **Template-driven forms** are useful for adding a simple form to an app, such as a single email list signup form. They are easy to add to an app, but they do not scale as well as reactive forms. If you have very basic form requirements and logic that can be managed solely in the template, use template-driven forms. + +## A common foundation + +Both reactive and template-driven forms share underlying building blocks, the `FormControl`, `FormGroup` and `FormArray`. A `FormControl` instance tracks the value and validation status of an individual form control element, a `FormGroup` instance tracks the same values and statuses for a collection, and a `FormArray` instance tracks the same values and statues for an array of form controls. How these control instances are created and managed with reactive and template-driven forms will be discussed later in this guide. + +## Control value accessors + +Control value accessors define a bridge between Angular forms and native DOM elements. The `ControlValueAccessor` interface defines methods for interacting with native elements including: reading from and writing values to them, disabling or enabling their elements, and providing callback functions for when the control's value changes in the UI or becomes touched. A built-in accessor is attached to every form field when using either forms module in Angular, unless a custom value accessor has been activated on that field. + +## Data flow in forms + +When building forms in Angular, it's important to understand how the the framework handles data flowing from the user or from programmatic changes. Reactive and template-driven follow two different strategies when handling these scenarios. Using a simple component with a single input field, we can illustrate how changes are handled. + +### Data flow in reactive forms + +Here is a component with an input field for a single control using reactive forms. + + + + +Let’s look at how the data flows for an input with reactive forms. + +**Diagram of Input Event Flow For Reactive Forms** + +In reactive forms, the source of truth is the form model (in this case, the `FormControl` instance), which is explicitly defined in the component class. This model is created independently of the UI and can be used to provide an initial value for the control. The reactive form directive (in this case, `FormControlDirective`) then links the existing form control instance to a specific form element in the view using a value accessor. + +When text is entered into the input field, the field's value accessor immediately relays the new value to the FormControl instance, which then emits the value through the valueChanges observable. + +With reactive forms, you have full control over the form model without ever rendering the UI. The source of truth is always up-to-date, because it is synchronously updated at the time changes are made. + +### Data flow in template-driven forms + +Here is a component with an input field for a single control using template-driven forms. + + + + +Let’s compare the same data flows with template-driven forms. + +**Diagram of Input Event Flow For Template-driven Forms** + +In template-driven forms, the source of truth is the template, so developers create their desired form through the placement of template-driven directives such as `NgModel` and `NgModelGroup`. The directives then create the `FormControl` or `FormGroup` instances that make up the form model, link them with form elements through a value accessor, and manage them within the constraints of the template's change detection cycle. + +This abstraction promotes simplicity over structure. It is less explicit, but you no longer have direct control over the form model. It is simple to add directives, but because these directives are dependent on the UI, they must work around the change detection cycle. Programmatic value changes are registered during change detection (as they occur through an `Input` to a directive), so it's not possible to update the value and validity immediately because it may affect elements that have already been checked in that view, for example, the parent form. For this reason, value changes are delayed until the next tick, which allows change detection to complete and a new change detection process to be triggered with the updated value. In other words, the process happens asynchronously and introduces unpredictability for querying the source of truth. + +## Custom validation + +Validation 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. With reactive forms, custom validators are functions that receive a control to validate. Because template-driven forms are tied to directives, custom validator directives must be created to wrap a validator function in order to use it in a template. + +For more on form validation, visit the [Form Validation](guide/form-validation) guide. + +## Testing + +Testing also plays a large part in complex applications and an easier testing strategy is always welcomed. One big difference in testing reactive forms and template-driven forms is their reliance on rendering the UI in order to perform assertions based on form control and form field changes. The following examples display the process of testing forms with reactive and template-driven forms. + +### Testing a reactive form + +Using the name components mentioned earlier, we test against the same data flows with reactive forms. + + + + +Reactive forms provide a relatively easy testing strategy to due to synchronous access to the form and data models, and being independent of the UI. In these set of tests, controls and data are queried and manipulated easily through the control without interactving with the change detection cycle. We make verify changes predictablity from the form model. + +### Testing a template-driven form + +Writing tests with template-driven forms is more involved and requires more detailed knowledge of the change detection process and how directives run on each cycle to ensure elements can be queried at the correct time. + + + + +Because template-driven are asynchronous, each test must be wrapped in a `fakeAsync` method to simulate queueing of tasks. The `tick` function *must* be used to update the rendered template after detecting changes. You must wait for the appropiate lifecycle hooks to finish before extracting any values, testing its validity or changing the value of a control. + +## Mutability + +How changes are tracked plays a role in the efficiency of your application. Reactive forms keep the data model pure by providing it as an immutable data structure. Each time the a change is triggered on the data model, a new data model is returned rather than updating the data model directly. This is more efficient, giving you the ability track unique changes to the data model. It also follows reactive patterns that integrate with observable operators to map and transform data. Template-driven forms rely on mutability with two-way data binding to update the data model in the component as changes are made in the template. + +## Scalability + +If forms are a central part of your application, scalability is very important. Being able to reuse form models across components and data access is critical. Reactive forms makes creating large scale forms easier by providing access to low-level APIs and synchronous access to the data model. Because template-driven forms focus on simplicity and simple scenarios with static content and little validation, they are not as reusable and abstract away the low-level APIs and access to the data model is handled asynchronously. + ## Next Steps After you understand the two approaches to handling form inputs, you can learn more about common examples and practices using reactive forms or template-driven forms. The following guides are the next steps in the learning process for each approach. diff --git a/aio/content/navigation.json b/aio/content/navigation.json index 0447cf33f784d..843650973c23a 100644 --- a/aio/content/navigation.json +++ b/aio/content/navigation.json @@ -163,6 +163,11 @@ "title": "Template Syntax", "tooltip": "Learn how to write templates that display data and consume user events with the help of data binding." }, + { + "url": "guide/user-input", + "title": "User Input", + "tooltip": "User input triggers DOM events. We listen to those events with event bindings that funnel updated values back into our components and models." + }, { "url": "guide/lifecycle-hooks", "title": "Lifecycle Hooks", @@ -214,11 +219,6 @@ "url": "guide/forms-overview", "title": "Forms Overview", "tooltip": "A form creates a cohesive, effective, and compelling data entry experience. An Angular form coordinates a set of data-bound user controls, tracks changes, validates input, and presents errors." - }, - { - "url": "guide/user-input", - "title": "User Input", - "tooltip": "User input triggers DOM events. We listen to those events with event bindings that funnel updated values back into our components and models." }, { "url": "guide/reactive-forms",