Performance: FormControl basics
FormControl affects how user input updates the UI and triggers validation, impacting interaction responsiveness and rendering.
const control = new FormControl(''); control.valueChanges.pipe(debounceTime(300)).subscribe(() => { updateUI(); });
const control = new FormControl(''); control.valueChanges.subscribe(() => { // heavy logic or UI updates on every keystroke updateUI(); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Immediate valueChanges subscription | Many updates per keystroke | Multiple reflows | High paint cost | [X] Bad |
| Debounced valueChanges subscription | Fewer updates | Single or few reflows | Lower paint cost | [OK] Good |
FormControl in Angular?FormControl is designed to track the value and validation status of one input field.FormGroup or FormArray, it does not manage multiple inputs or groups.FormControl with an initial value of 'hello'?FormControl is created by calling new FormControl(initialValue).const nameControl = new FormControl('Alice');
nameControl.setValue('Bob');
console.log(nameControl.value);FormControl is created with initial value 'Alice'.setValue('Bob') updates the control's value to 'Bob'.nameControl.value outputs the updated value 'Bob'.const ageControl = new FormControl(); ageControl.setValue;
ageControl.setValue without parentheses, so the method is referenced but not called.setValue is not called, no value is set and no error occurs, but the intended update does not happen.FormControl that starts empty but requires the user to enter a value. Which code snippet correctly sets this up with validation?Validators.required.Pass the test and this pattern moves unknown