this.myForm.value after initialization?import { Component, inject } from '@angular/core'; import { FormBuilder } from '@angular/forms'; @Component({ selector: 'app-sample', template: ``, standalone: true }) export class SampleComponent { fb = inject(FormBuilder); myForm = this.fb.group({ firstName: ['John'], lastName: ['Doe'] }); }
The FormBuilder group method creates a form group with controls initialized to the values inside the arrays. So the form value is an object with keys and their initial values.
The correct syntax for validators in FormBuilder is to pass an array of validators as the second element in the control array. Option A correctly wraps Validators.required in an array.
this.myForm.patchValue({ username: 'alice' }) is called?import { Component, inject } from '@angular/core'; import { FormBuilder } from '@angular/forms'; @Component({ selector: 'app-user', template: ``, standalone: true }) export class UserComponent { fb = inject(FormBuilder); myForm = this.fb.group({ username: ['bob'], age: [30] }); updateUsername() { this.myForm.patchValue({ username: 'alice' }); } }
The patchValue method updates the specified control's value. Here, 'username' changes from 'bob' to 'alice'.
import { Component, inject } from '@angular/core'; import { FormBuilder, Validators } from '@angular/forms'; @Component({ selector: 'app-login', template: ``, standalone: true }) export class LoginComponent { fb = inject(FormBuilder); loginForm = this.fb.group({ email: ['', Validators.required, Validators.email] }); }
The FormBuilder control array expects the second element to be a single validator or an array of validators. Passing multiple validators as separate arguments causes an error.
this.profileForm.get('address.street')?import { Component, inject } from '@angular/core'; import { FormBuilder } from '@angular/forms'; @Component({ selector: 'app-profile', template: ``, standalone: true }) export class ProfileComponent { fb = inject(FormBuilder); profileForm = this.fb.group({ name: [''], address: this.fb.control({ street: [''], city: [''] }) }); }
The code uses this.fb.control to create 'address', which makes it a FormControl holding an object, not a FormGroup. So get('address.street') returns null.