2020-09-17 17:10:16 +00:00
# Using forms for user input
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
This guide builds on the [Managing Data ](start/start-data "Try it: Managing Data" ) step of the Getting Started tutorial, [Get started with a basic Angular app ](start "Get started with a basic Angular app" ).
2019-03-20 15:10:47 +00:00
2019-09-24 19:23:24 +00:00
This section walks you through adding a form-based checkout feature to collect user information as part of checkout.
2019-03-20 15:10:47 +00:00
## Define the checkout form model
2020-11-13 21:09:26 +00:00
This step shows you how to set up the checkout form model in the component class.
The form model determines the status of the form.
2019-03-20 15:10:47 +00:00
1. Open `cart.component.ts` .
2020-11-13 21:09:26 +00:00
1. Import the `FormBuilder` service from the `@angular/forms` package.
This service provides convenient methods for generating controls.
2019-03-20 15:10:47 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/app/cart/cart.component.ts" path = "getting-started/src/app/cart/cart.component.ts" region = "imports" > < / code-example >
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
1. Inject the `FormBuilder` service in the `CartComponent` `constructor()` .
This service is part of the `ReactiveFormsModule` module, which you've already imported.
2019-03-20 15:10:47 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/app/cart/cart.component.ts" path = "getting-started/src/app/cart/cart.component.ts" region = "inject-form-builder" > < / code-example >
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
1. To gather the user's name and address, use the `FormBuilder` `group()` method to set the `checkoutForm` property to a form model containing `name` and `address` fields.
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
< code-example header = "src/app/cart/cart.component.ts" path = "getting-started/src/app/cart/cart.component.ts" region = "checkout-form-group" > < / code-example >
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
1. Define an `onSubmit()` method to process the form.
This method allows users to submit their name and address.
In addition, this method uses the `clearCart()` method of the `CartService` to reset the form and clear the cart.
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
The entire cart component class is as follows:
2019-03-20 15:10:47 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/app/cart/cart.component.ts" path = "getting-started/src/app/cart/cart.component.ts" > < / code-example >
2019-03-20 15:10:47 +00:00
## Create the checkout form
2020-11-13 21:09:26 +00:00
Use the following steps to add a checkout form at the bottom of the Cart view.
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
1. At the bottom of `cart.component.html` , add an HTML `<form>` element and a **Purchase** button.
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
1. Use a `formGroup` property binding to bind `checkoutForm` to the HTML `<form>` .
2019-03-20 15:10:47 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/app/cart/cart.component.html" path = "getting-started/src/app/cart/cart.component.3.html" region = "checkout-form" > < / code-example >
2019-03-20 15:10:47 +00:00
1. On the `form` tag, use an `ngSubmit` event binding to listen for the form submission and call the `onSubmit()` method with the `checkoutForm` value.
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/app/cart/cart.component.html (cart component template detail)" path = "getting-started/src/app/cart/cart.component.html" region = "checkout-form-1" > < / code-example >
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
1. Add `<input>` fields for `name` and `address` , each with a `formControlName` attribute that binds to the `checkoutForm` form controls for `name` and `address` to their `<input>` fields.
The complete component is as follows:
2019-03-20 15:10:47 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/app/cart/cart.component.html" path = "getting-started/src/app/cart/cart.component.html" region = "checkout-form-2" > < / code-example >
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
After putting a few items in the cart, users can review their items, enter their name and address, and submit their purchase.
2019-03-20 15:10:47 +00:00
2019-11-11 22:47:51 +00:00
< div class = "lightbox" >
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< img alt = "Cart view with checkout form" src = "generated/images/guide/start/cart-with-items-and-form.png" >
2019-11-11 22:47:51 +00:00
< / div >
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
To confirm submission, open the console to see an object containing the name and address you submitted.
## What's next
2019-03-20 15:10:47 +00:00
2020-11-13 21:09:26 +00:00
You have a complete online store application with a product catalog, a shopping cart, and a checkout function.
2019-03-20 15:10:47 +00:00
2020-02-18 18:14:48 +00:00
[Continue to the "Deployment" section ](start/start-deployment "Try it: Deployment" ) to move to local development, or deploy your app to Firebase or your own server.
2021-09-15 21:09:19 +00:00
@reviewed 2021-09-15