0
0
Angularframework~20 mins

FormControl basics in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FormControl Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the initial value of this FormControl?
Consider the following Angular code snippet creating a FormControl. What will be the initial value of nameControl.value?
Angular
import { FormControl } from '@angular/forms';

const nameControl = new FormControl('Alice');
A'' (empty string)
B'Alice'
Cundefined
Dnull
Attempts:
2 left
💡 Hint
Check what value is passed when creating the FormControl.
state_output
intermediate
2:00remaining
What is the value of emailControl.value after update?
Given this Angular FormControl, what will be the value of emailControl.value after calling setValue('user@example.com')?
Angular
import { FormControl } from '@angular/forms';

const emailControl = new FormControl('');
emailControl.setValue('user@example.com');
Anull
Bundefined
C'user@example.com'
D'' (empty string)
Attempts:
2 left
💡 Hint
setValue changes the FormControl's value.
lifecycle
advanced
2:00remaining
What happens when markAsTouched() is called on a FormControl?
In Angular, after calling markAsTouched() on a FormControl, which of the following is true?
Angular
import { FormControl } from '@angular/forms';

const control = new FormControl('');
control.markAsTouched();
AThe control's <code>untouched</code> property becomes true
BThe control's <code>dirty</code> property becomes true
CThe control's <code>pristine</code> property becomes true
DThe control's <code>touched</code> property becomes true
Attempts:
2 left
💡 Hint
Touched means the user has focused and left the control.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a FormControl with a required validator?
Select the correct Angular code to create a FormControl with an initial empty string and a required validator.
Angular
import { Validators } from '@angular/forms';
Anew FormControl('', [Validators.required])
Bnew FormControl('', Validators.required)
Cnew FormControl(Validators.required, '')
Dnew FormControl([Validators.required], '')
Attempts:
2 left
💡 Hint
Validators go as the second argument in an array.
🔧 Debug
expert
3:00remaining
Why does this FormControl's value not update on input?
Given this Angular template and component code, why does the FormControl's value not update when typing in the input field? Component: import { Component } from '@angular/core'; import { FormControl } from '@angular/forms'; @Component({ selector: 'app-email', templateUrl: './email.component.html' }) export class EmailComponent { email = new FormControl(''); } Template:
Angular
<input type="email" [formControl]="email" (input)="email.setValue($event.target.value)" />
AThe (input) event handler causes a loop and prevents proper update
BThe FormControl is missing a validator
CThe input type should be text, not email
DThe FormControl must be initialized with null, not empty string
Attempts:
2 left
💡 Hint
Using both [formControl] and (input) with setValue causes conflicts.