Performance: Form state tracking (dirty, touched, valid)
This affects interaction responsiveness and rendering speed during user input in forms.
this.form.controls['name'].valueChanges.pipe(debounceTime(300)).subscribe(() => { if (this.form.controls['name'].dirty) { this.showDirtyState(this.form.controls['name']); } });
this.form.valueChanges.subscribe(() => { this.updateUI(); }); updateUI() { Object.values(this.form.controls).forEach(control => { if (control.dirty) { this.showDirtyState(control); } }); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Subscribe to all form valueChanges and update UI immediately | High (all controls checked) | Multiple per keystroke | High (many style/layout recalculations) | [X] Bad |
| Subscribe to specific controls with debounce and conditional updates | Low (targeted controls only) | Single per pause | Low (minimal style/layout recalculations) | [OK] Good |
dirty state indicate about a form control?dirty in Angular formsdirty state means the user has changed the value of the input from its original value.touched means the user focused and left the input, valid means the input passes validation, and disabled means the control is inactive.email is touched in an Angular template?form.controls.controlName or form.get('controlName'). The touched property is a boolean, not a method.form.controls.email.touched correctly accesses the touched property. Other options incorrectly use method calls or wrong property names.this.form = new FormGroup({
username: new FormControl('', Validators.required)
});
// User focuses on username input, types 'John', then leaves the input.dirty, touched, and valid for username after these actions?touched is true. User changed the value from empty string to 'John', so dirty is true.Validators.required. Since 'John' is not empty, the control is valid, so valid is true.<input type="text" formControlName="email">
<div *ngIf="form.controls.email.dirty && form.controls.email.valid">Valid email changed!</div>
dirty and valid.valid is false, so the message won't appear.<button [disabled]="???">Submit</button>
pristine means no changes made, opposite of dirty. invalid means form is not valid.form.pristine || form.invalid.Pass the test and this pattern moves unknown