A <code>FormControl</code> is a class that tracks the value and validation status of an individual form input element. It helps manage user input and form state.
Click to reveal answer
beginner
How do you create a simple FormControl in Angular?
You create it by importing <code>FormControl</code> from <code>@angular/forms</code> and then initializing it like this: <code>const nameControl = new FormControl('');</code> This sets up a control with an empty initial value.
Click to reveal answer
beginner
How can you get the current value of a FormControl?
You access the value property of the FormControl. For example, nameControl.value returns the current input value.
Click to reveal answer
beginner
What is the purpose of validators in a FormControl?
Validators check if the input value meets certain rules, like required fields or minimum length. They help keep the form data correct and useful.
Click to reveal answer
intermediate
How do you listen for changes in a FormControl?
You subscribe to the valueChanges observable. For example: nameControl.valueChanges.subscribe(value => console.log(value)); This runs code whenever the input changes.
Click to reveal answer
What does a FormControl track in Angular?
AThe value and validation status of a form input
BThe layout of the form
CThe routing of the application
DThe CSS styles of the form
✗ Incorrect
A FormControl tracks the value and validation status of a single form input.
How do you create a new FormControl with an initial value of 'hello'?
Anew FormControl()
Bnew FormGroup('hello')
Cnew FormControl('hello')
Dnew FormBuilder('hello')
✗ Incorrect
You create a FormControl by calling new FormControl('hello') to set the initial value.
Which property gives you the current value of a FormControl?
Avalue
Bstatus
Cvalid
Derrors
✗ Incorrect
The value property holds the current input value of the FormControl.
What is the role of validators in a FormControl?
ATo reset the form
BTo check if input meets rules like required or length
CTo submit the form automatically
DTo style the input field
✗ Incorrect
Validators ensure the input follows rules such as being required or having a minimum length.
How do you react to changes in a FormControl value?
AUse <code>setValue()</code>
BUse <code>ngOnInit</code>
CCall <code>reset()</code>
DSubscribe to <code>valueChanges</code>
✗ Incorrect
You subscribe to the valueChanges observable to run code when the input changes.
Explain what a FormControl is and how you create one in Angular.
Think about managing a single input field's data and rules.
You got /3 concepts.
Describe how you can check the current value and listen for changes in a FormControl.
One is a property, the other is an observable.
You got /2 concepts.
Practice
(1/5)
1. What is the primary purpose of FormControl in Angular?
easy
A. To create a complete form with multiple inputs
B. To manage the value and validation state of a single form input
C. To style form elements with CSS
D. To handle HTTP requests in forms
Solution
Step 1: Understand FormControl role
FormControl is designed to track the value and validation status of one input field.
Step 2: Differentiate from other form classes
Unlike FormGroup or FormArray, it does not manage multiple inputs or groups.
Final Answer:
To manage the value and validation state of a single form input -> Option B
Quick Check:
FormControl = single input manager [OK]
Hint: FormControl handles one input's value and validation [OK]
Common Mistakes:
Confusing FormControl with FormGroup
Thinking FormControl handles styling
Assuming FormControl manages HTTP
2. Which of the following is the correct way to create a FormControl with an initial value of 'hello'?
easy
A. new FormControl('hello')
B. FormControl('hello')
C. new FormControl = 'hello'
D. FormControl.create('hello')
Solution
Step 1: Recall FormControl instantiation syntax
In Angular, FormControl is created by calling new FormControl(initialValue).
Step 2: Check each option
new FormControl('hello') uses the correct constructor syntax. Options A, B, and D are invalid or incorrect syntax.
Final Answer:
new FormControl('hello') -> Option A
Quick Check:
Use new keyword with FormControl constructor [OK]
Hint: Use new FormControl(value) to create a control [OK]
Common Mistakes:
Omitting new keyword
Using assignment instead of constructor
Calling non-existent create method
3. Given this code snippet:
const nameControl = new FormControl('Alice');
nameControl.setValue('Bob');
console.log(nameControl.value);
What will be logged to the console?
medium
A. undefined
B. 'Alice'
C. 'Bob'
D. Error: setValue is not a function
Solution
Step 1: Understand initial value assignment
The FormControl is created with initial value 'Alice'.
Step 2: Apply setValue method
The setValue('Bob') updates the control's value to 'Bob'.
Step 3: Check logged value
Logging nameControl.value outputs the updated value 'Bob'.
Final Answer:
'Bob' -> Option C
Quick Check:
setValue changes value immediately [OK]
Hint: setValue updates FormControl value instantly [OK]
Common Mistakes:
Assuming initial value stays after setValue
Thinking setValue is asynchronous
Confusing value property with method
4. Identify the error in this code:
const ageControl = new FormControl();
ageControl.setValue;
medium
A. setValue is called without parentheses, so no value is set
B. FormControl must have an initial value
C. setValue cannot be used on FormControl
D. ageControl is not declared properly
Solution
Step 1: Check setValue usage
The code uses ageControl.setValue without parentheses, so the method is referenced but not called.
Step 2: Understand consequences
Because setValue is not called, no value is set and no error occurs, but the intended update does not happen.
Final Answer:
setValue is called without parentheses, so no value is set -> Option A
Quick Check:
Call setValue() with parentheses to update value [OK]
Hint: Always use parentheses to call setValue() [OK]
Common Mistakes:
Forgetting parentheses when calling methods
Thinking FormControl needs initial value
Misunderstanding method vs property
5. You want to create a FormControl that starts empty but requires the user to enter a value. Which code snippet correctly sets this up with validation?
hard
A. new FormControl('', { required: true })
B. new FormControl('', required)
C. new FormControl(Validators.required, '')
D. new FormControl('', [Validators.required])
Solution
Step 1: Recall FormControl constructor signature
The constructor accepts initial value as first argument, and validators as second argument in an array.
Step 2: Analyze each option
new FormControl('', { required: true }) uses invalid options object. new FormControl('', required) references undefined required. new FormControl(Validators.required, '') swaps arguments incorrectly. new FormControl('', [Validators.required]) correctly passes an array with Validators.required.
Final Answer:
new FormControl('', [Validators.required]) -> Option D
Quick Check:
Validators go in array as second argument [OK]
Hint: Pass validators as array in second argument [OK]
Common Mistakes:
Confusing with template-driven {required: true}
Forgetting Validators prefix
Swapping argument order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.