FormControl helps you manage and track the value and status of a single form input in Angular. It makes handling user input easier and organized.
FormControl basics in Angular
const control = new FormControl(initialValue, validators);
initialValue is the starting value of the input (can be empty).
validators are optional rules to check input correctness.
const nameControl = new FormControl('');
const ageControl = new FormControl(18);
const emailControl = new FormControl('', Validators.required);
const passwordControl = new FormControl('', [Validators.required, Validators.minLength(6)]);
This Angular component creates a FormControl for a username input. It requires the username to be filled. If the user touches the input and leaves it empty, an error message shows. The current input value is displayed below.
import { Component } from '@angular/core'; import { FormControl, Validators } from '@angular/forms'; @Component({ selector: 'app-simple-input', template: ` <label for="username">Username:</label> <input id="username" [formControl]="usernameControl" aria-describedby="error" /> <div *ngIf="usernameControl.invalid && usernameControl.touched" id="error" style="color: red;"> Username is required. </div> <p>Your input: {{ usernameControl.value }}</p> ` }) export class SimpleInputComponent { usernameControl = new FormControl('', Validators.required); }
Use formControl.value to get the current input value.
Use formControl.invalid and formControl.touched to show validation messages only after user interaction.
You can reset the control with formControl.reset() to clear the value and status.
FormControl manages one input's value and validation.
It helps track user input and show errors easily.
Use it for simple inputs or inside bigger forms.
Practice
FormControl in Angular?Solution
Step 1: Understand FormControl role
FormControlis designed to track the value and validation status of one input field.Step 2: Differentiate from other form classes
UnlikeFormGrouporFormArray, it does not manage multiple inputs or groups.Final Answer:
To manage the value and validation state of a single form input -> Option BQuick Check:
FormControl = single input manager [OK]
- Confusing FormControl with FormGroup
- Thinking FormControl handles styling
- Assuming FormControl manages HTTP
FormControl with an initial value of 'hello'?Solution
Step 1: Recall FormControl instantiation syntax
In Angular,FormControlis created by callingnew FormControl(initialValue).Step 2: Check each option
new FormControl('hello') uses the correct constructor syntax. Options A, B, and D are invalid or incorrect syntax.Final Answer:
new FormControl('hello') -> Option AQuick Check:
Use new keyword with FormControl constructor [OK]
- Omitting new keyword
- Using assignment instead of constructor
- Calling non-existent create method
const nameControl = new FormControl('Alice');
nameControl.setValue('Bob');
console.log(nameControl.value);What will be logged to the console?
Solution
Step 1: Understand initial value assignment
TheFormControlis created with initial value 'Alice'.Step 2: Apply setValue method
ThesetValue('Bob')updates the control's value to 'Bob'.Step 3: Check logged value
LoggingnameControl.valueoutputs the updated value 'Bob'.Final Answer:
'Bob' -> Option CQuick Check:
setValue changes value immediately [OK]
- Assuming initial value stays after setValue
- Thinking setValue is asynchronous
- Confusing value property with method
const ageControl = new FormControl(); ageControl.setValue;
Solution
Step 1: Check setValue usage
The code usesageControl.setValuewithout parentheses, so the method is referenced but not called.Step 2: Understand consequences
BecausesetValueis not called, no value is set and no error occurs, but the intended update does not happen.Final Answer:
setValue is called without parentheses, so no value is set -> Option AQuick Check:
Call setValue() with parentheses to update value [OK]
- Forgetting parentheses when calling methods
- Thinking FormControl needs initial value
- Misunderstanding method vs property
FormControl that starts empty but requires the user to enter a value. Which code snippet correctly sets this up with validation?Solution
Step 1: Recall FormControl constructor signature
The constructor accepts initial value as first argument, and validators as second argument in an array.Step 2: Analyze each option
new FormControl('', { required: true }) uses invalid options object. new FormControl('', required) references undefined required. new FormControl(Validators.required, '') swaps arguments incorrectly. new FormControl('', [Validators.required]) correctly passes an array withValidators.required.Final Answer:
new FormControl('', [Validators.required]) -> Option DQuick Check:
Validators go in array as second argument [OK]
- Confusing with template-driven {required: true}
- Forgetting Validators prefix
- Swapping argument order
Ready to prove it?
Pass the test and this pattern moves unknown
