Form submission handling lets your app collect and use information users type in forms. It helps you respond when users click submit.
Form submission handling in Angular
import { Component } from '@angular/core'; import { FormsModule } from '@angular/forms'; @Component({ selector: 'app-simple-form', standalone: true, imports: [FormsModule], template: ` <form (ngSubmit)="onSubmit()" #formRef="ngForm"> <input name="username" [(ngModel)]="username" required aria-label="Username" /> <button type="submit" [disabled]="formRef.invalid">Submit</button> </form> ` }) export class SimpleFormComponent { username = ''; onSubmit() { // handle form data here } }
Use (ngSubmit) on the <form> tag to catch the submit event.
Use [(ngModel)] to bind input values to component properties.
<form (ngSubmit)="submitForm()"> <input name="email" [(ngModel)]="email" required aria-label="Email" /> <button type="submit">Send</button> </form>
<form (ngSubmit)="save()" #myForm="ngForm"> <input name="age" type="number" [(ngModel)]="age" min="1" max="120" aria-label="Age" /> <button type="submit" [disabled]="myForm.invalid">Save</button> </form>
This component shows a form with name and email fields. The submit button is disabled until both fields are valid. When submitted, it shows a thank you message with the entered data.
import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; @Component({ selector: 'app-user-form', standalone: true, imports: [CommonModule, FormsModule], template: ` <form (ngSubmit)="onSubmit()" #userForm="ngForm"> <label for="name">Name:</label> <input id="name" name="name" [(ngModel)]="name" required aria-label="Name" /> <label for="email">Email:</label> <input id="email" name="email" [(ngModel)]="email" required email aria-label="Email" /> <button type="submit" [disabled]="userForm.invalid">Submit</button> </form> <p *ngIf="submitted">Thank you, {{ name }}! We received your email: {{ email }}.</p> ` }) export class UserFormComponent { name = ''; email = ''; submitted = false; onSubmit() { this.submitted = true; } }
Always add aria-label or label elements for accessibility.
Use Angular's form validation to prevent bad data submission.
Use template reference variables like #userForm="ngForm" to check form state.
Use (ngSubmit) to handle form submission in Angular.
Bind inputs with [(ngModel)] for two-way data binding.
Disable submit button when form is invalid to improve user experience.
Practice
(ngSubmit) directive in Angular forms?Solution
Step 1: Understand the role of (ngSubmit)
The (ngSubmit) directive listens for the submit event on a form element in Angular.Step 2: Identify what happens on submit
When the form is submitted, (ngSubmit) triggers the specified handler function in the component.Final Answer:
It listens for the form's submit event and triggers a handler function. -> Option BQuick Check:
(ngSubmit) handles form submission event [OK]
- Confusing (ngSubmit) with input binding
- Thinking (ngSubmit) disables buttons
- Assuming (ngSubmit) styles forms
username using two-way binding in Angular?Solution
Step 1: Recall two-way binding syntax
Angular uses[(ngModel)]for two-way data binding on form inputs.Step 2: Match syntax to property
The correct syntax to bindusernameis<input [(ngModel)]="username">.Final Answer:
<input [(ngModel)]="username"> -> Option AQuick Check:
Two-way binding = [(ngModel)] [OK]
- Using only [ngModel] without parentheses
- Using (ngModelChange) alone without brackets
- Confusing event binding with property binding
submittedName after the form is submitted?<form (ngSubmit)="submitForm()" #myForm="ngForm"> <input name="name" [(ngModel)]="userName" required> <button type="submit" [disabled]="myForm.invalid">Submit</button> </form>
And the component code:
userName = '';
submittedName = '';
submitForm() {
this.submittedName = this.userName;
}If the user types "Alice" and submits, what is
submittedName?Solution
Step 1: Understand two-way binding effect
The input field bindsuserNamewith two-way binding, so typing "Alice" updatesuserNameto "Alice".Step 2: Check submitForm behavior
On submit,submitForm()setssubmittedNameto the currentuserName, which is "Alice".Final Answer:
"Alice" -> Option CQuick Check:
Two-way binding updates userName, submitForm copies it [OK]
- Assuming submittedName stays empty
- Confusing variable names
- Ignoring two-way binding effect
<form (ngSubmit)="onSubmit"> <input [(ngModel)]="email" name="email" required> <button type="submit">Send</button> </form>
Component method:
onSubmit() {
console.log('Form submitted:', this.email);
}Solution
Step 1: Check (ngSubmit) syntax
The (ngSubmit) directive requires a function call with parentheses to execute the method on submit.Step 2: Identify the missing parentheses
Using (ngSubmit)="onSubmit" passes the function reference but does not call it; it should be (ngSubmit)="onSubmit()".Final Answer:
The (ngSubmit) should be (ngSubmit)="onSubmit()" with parentheses. -> Option AQuick Check:
Call handler with () in (ngSubmit) [OK]
- Omitting parentheses in (ngSubmit)
- Ignoring required name attribute
- Thinking input type is mandatory for submission
<form (ngSubmit)="submit()" #myForm="ngForm"> <input name="email" [(ngModel)]="email" required> <input name="password" [(ngModel)]="password" required minlength="6"> <button type="submit" ???>Submit</button> </form>
What should replace
??? to disable the button when the form is invalid?Solution
Step 1: Understand Angular form validity
The template reference variablemyFormexposes the form's validity state viamyForm.invalidandmyForm.valid.Step 2: Use form invalid property to disable button
Setting[disabled]="myForm.invalid"disables the submit button until the form is valid.Final Answer:
[disabled]="myForm.invalid" -> Option DQuick Check:
Disable button when form.invalid is true [OK]
- Using negation incorrectly with valid property
- Manually checking fields instead of form state
- Setting disabled to false always
Ready to prove it?
Pass the test and this pattern moves unknown
