When you want to manage your forms programmatically instead of relying purely on the template, reactive forms are the answer.
In this activity, you'll learn how to setup reactive forms.
- 
      
      
  ImportReactiveFormsmoduleIn app.component.ts, importReactiveFormsModulefrom@angular/formsand add it to theimportsarray of the component.import { ReactiveFormsModule } from '@angular/forms';@Component({ selector: 'app-root', standalone: true, template: ` <form> <label>Name <input type="text" /> </label> <label>Email <input type="email" /> </label> <button type="submit">Submit</button> </form> `, imports: [ReactiveFormsModule],})
- 
      
      
  Create theFormGroupobject with FormControlsReactive forms use the FormControlclass to represent the form controls (e.g., inputs). Angular provides theFormGroupclass to serve as a grouping of form controls into a helpful object that makes handling large forms more convenient for developers.Add FormControlandFormGroupto the import from@angular/formsso that you can create a FormGroup for each form, with the propertiesnameandemailas FormControls.import {ReactiveFormsModule, FormControl, FormGroup } from '@angular/forms';...export class AppComponent { profileForm = new FormGroup({ name: new FormControl(''), email: new FormControl(''), });}
- 
      
      
  Link the FormGroup and FormControls to the formEach FormGroupshould be attached to a form using the[formGroup]directive.In addition, each FormControlcan be attached with theformControlNamedirective and assigned to the corresponding property. Update the template with the following form code:<form [formGroup]="profileForm"> <label> Name <input type="text" formControlName="name" /> </label> <label> Email <input type="email" formControlName="email" /> </label> <button type="submit">Submit</button></form>
- 
      
      
  Handle update to the formWhen you want to access data from the FormGroup, it can be done by accessing the value of theFormGroup. Update thetemplateto display the form values:...<h2>Profile Form</h2><p>Name: {{ profileForm.value.name }}</p><p>Email: {{ profileForm.value.email }}</p>
- 
      
      
  Access FormGroup valuesAdd a new method to the component class called handleSubmitthat you'll later use to handle the form submission. This method will display values from the form, you can access the values from the FormGroup.In the component class, add the handleSubmit()method to handle the form submission.handleSubmit() { alert( this.profileForm.value.name + ' | ' + this.profileForm.value.email );}
- 
      
      
  AddngSubmitto the formYou have access to the form values, now it is time to handle the submission event and use the handleSubmitmethod. Angular has an event handler for this specific purpose calledngSubmit. Update the form element to call thehandleSubmitmethod when the form is submitted.<form [formGroup]="profileForm" (ngSubmit)="handleSubmit()">
And just like that, you know how to work with reactive forms in Angular.
Fantastic job with this activity. Keep going to learn about form validation.