In Angular, reactive forms offer advantages over template-driven forms. Which reason best explains why reactive forms are preferred for complex validation?
Think about how reactive forms handle form data and validation logic.
Reactive forms keep the form model in the component class, allowing synchronous and explicit control over validation logic, which is ideal for complex scenarios.
Consider an Angular reactive form. What is the expected behavior when you update a form control's value using setValue() method?
Think about how reactive forms handle programmatic changes.
Using setValue() updates the control's value immediately and triggers validation synchronously.
Which code snippet correctly creates a reactive form group with controls name and email using Angular's FormBuilder?
import { FormBuilder, FormGroup } from '@angular/forms'; constructor(private fb: FormBuilder) {} ngOnInit() { this.myForm = /* fill here */; }
Remember that FormBuilder.group() creates a form group with controls.
FormBuilder.group() takes an object with control names and initial values or validators. Option D correctly uses this.
Given this Angular reactive form code, why does the UI not reflect changes when setValue() is called?
this.myForm = this.fb.group({ username: [''] });
// Later in code
this.myForm.controls['username'].setValue('newUser');Check how the form control connects to the HTML template.
If the form control is not linked in the template with formControlName, UI won't update even if the value changes in code.
In an Angular component using reactive forms, which lifecycle hook is the best place to initialize the form group and why?
Consider when component inputs are available and when initialization should happen.
ngOnInit() is the recommended place to initialize reactive forms because inputs are set and the component is ready to set up the form model before rendering.