4.8 KiB
Getting Started with Angular: Forms
At the end of Managing Data, the online store application has a product catalog and a shopping cart.
This section walks you through adding a form-based checkout feature to collect user information as part of checkout.
Forms in Angular
Forms in Angular build upon the standard HTML forms to help you create custom form controls and easy validation experiences. There are two parts to an Angular Reactive form: the objects that live in the component to store and manage the form, and the visualization of the form that lives in the template.
Define the checkout form model
First, set up the checkout form model. Defined in the component class, the form model is the source of truth for the status of the form.
-
Open
cart.component.ts
. -
Angular's
FormBuilder
service provides convenient methods for generating controls. As with the other services you've used, you need to import and inject the service before you can use it:- Import the
FormBuilder
service from the@angular/forms
package.
The
ReactiveFormsModule
provides theFormBuilder
service, whichAppModule
(inapp.module.ts
) already imports.- Inject the
FormBuilder
service.
- Import the
-
Still in the
CartComponent
class, define thecheckoutForm
property to store the form model. -
To gather the user's name and address, set the
checkoutForm
property with a form model containingname
andaddress
fields, using theFormBuilder
group()
method. Add this between the curly braces,{}
, of the constructor. -
For the checkout process, users need to submit their name and address. When they submit their order, the form should reset and the cart should clear.
- In
cart.component.ts
, define anonSubmit()
method to process the form. Use theCartService
clearCart()
method to empty the cart items and reset the form after its submission. In a real-world app, this method would also submit the data to an external server. The entire cart component class is as follows:
- In
Now that you've defined the form model in the component class, you need a checkout form to reflect the model in the view.
Create the checkout form
Use the following steps to add a checkout form at the bottom of the "Cart" page.
-
Open
cart.component.html
. -
At the bottom of the template, add an HTML form to capture user information.
-
Use a
formGroup
property binding to bind thecheckoutForm
to theform
tag in the template. Also include a "Purchase" button to submit the form.
- On the
form
tag, use anngSubmit
event binding to listen for the form submission and call theonSubmit()
method with thecheckoutForm
value.
- Add input fields for
name
andaddress
. Use theformControlName
attribute binding to bind thecheckoutForm
form controls forname
andaddress
to their input fields. The final complete component is as follows:
After putting a few items in the cart, users can now review their items, enter their name and address, and submit their purchase:
To confirm submission, open the console where you should see an object containing the name and address you submitted.
Next steps
Congratulations! You have a complete online store application with a product catalog, a shopping cart, and a checkout function.
Continue to the "Deployment" section to move to local development, or deploy your app to Firebase or your own server.