Consider an Angular component using ReactiveFormsModule with a form control marked as Validators.required. What will be the form's valid status and error state after submitting the form without entering any value?
import { Component } from '@angular/core'; import { FormControl, Validators } from '@angular/forms'; @Component({ selector: 'app-simple-form', template: ` <form (ngSubmit)="onSubmit()"> <input [formControl]="nameControl" placeholder="Name" /> <button type="submit">Submit</button> </form> <p *ngIf="nameControl.invalid && nameControl.touched">Name is required.</p> ` }) export class SimpleFormComponent { nameControl = new FormControl('', Validators.required); onSubmit() { console.log('Form valid:', this.nameControl.valid); console.log('Errors:', this.nameControl.errors); } }
Think about what Validators.required does when the input is empty.
The Validators.required marks the control invalid if the value is empty. So the form control is invalid and the errors object contains { required: true }.
You want to use reactive forms in a standalone Angular component. Which import statement and component decorator setup is correct?
Standalone components require explicit imports of modules they use.
For standalone components, you must import ReactiveFormsModule in the imports array of the component decorator. Using FormsModule is for template-driven forms, not reactive forms.
Given a form group with two controls, what will be the value of the form after calling patchValue with only one control's value?
import { Component } from '@angular/core'; import { FormGroup, FormControl } from '@angular/forms'; @Component({ selector: 'app-patch-form', template: `<p>Check console for output</p>` }) export class PatchFormComponent { form = new FormGroup({ firstName: new FormControl('John'), lastName: new FormControl('Doe') }); constructor() { this.form.patchValue({ firstName: 'Jane' }); console.log(this.form.value); } }
Remember what patchValue does compared to setValue.
patchValue updates only the specified controls and leaves others unchanged. So firstName becomes 'Jane' and lastName remains 'Doe'.
In this Angular component using reactive forms, the input field does not update the form control's value when typing. What is the cause?
import { Component } from '@angular/core'; import { FormControl } from '@angular/forms'; @Component({ selector: 'app-input-debug', template: `<input [formControl]="nameControl" />` }) export class InputDebugComponent { nameControl = new FormControl(''); constructor() { this.nameControl.setValue('Initial'); } }
Check if the binding syntax matches reactive forms usage.
Using [formControl] on the input is the correct way to bind a FormControl in reactive forms. Setting the value in constructor does not block user input. The issue is not in the shown code.
You create a reactive form in a component but forget to import ReactiveFormsModule in your Angular module. What error or behavior will you observe when running the app?
Think about what Angular needs to recognize formControl in templates.
If ReactiveFormsModule is not imported, Angular does not recognize the formControl directive in templates and throws a template parse error.