0
0
Angularframework~20 mins

ngForm directive and form state in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Forms Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the initial state of an Angular form using ngForm?
Consider a simple Angular template-driven form using ngForm. What is the initial value of the form's valid and pristine properties right after the form loads?
Angular
<form #myForm="ngForm">
  <input name="email" ngModel required />
</form>
Avalid: false, pristine: true
Bvalid: true, pristine: false
Cvalid: false, pristine: false
Dvalid: true, pristine: true
Attempts:
2 left
💡 Hint
Think about required fields and whether the user has changed anything yet.
state_output
intermediate
2:00remaining
What is the form's dirty state after user input?
Given this Angular form, after the user types any character into the input field, what will be the value of myForm.dirty?
Angular
<form #myForm="ngForm">
  <input name="username" ngModel />
</form>
Anull
Btrue
Cundefined
Dfalse
Attempts:
2 left
💡 Hint
Dirty means the user has changed the form since it loaded.
📝 Syntax
advanced
2:00remaining
Which option correctly binds the ngForm directive to a template reference variable?
Select the correct syntax to create a template reference variable named formRef that references the ngForm directive on a form element.
A<form #formRef>
B<form #formRef="ngform">
C<form #formRef="form">
D<form #formRef="ngForm">
Attempts:
2 left
💡 Hint
Angular is case sensitive for directive export names.
🔧 Debug
advanced
2:00remaining
Why does this Angular form's valid property always stay true even with an empty required input?
Examine the code below. The input is marked as required, but the form's valid property remains true even when the input is empty. What is the cause?
Angular
<form #myForm="ngForm">
  <input name="email" required />
</form>
AThe input is missing the <code>ngModel</code> directive, so validation is not tracked.
BThe form reference variable is incorrectly named.
CThe <code>required</code> attribute is misspelled, so it is ignored.
DThe form tag is missing the <code>novalidate</code> attribute.
Attempts:
2 left
💡 Hint
Angular needs ngModel to track form control state.
🧠 Conceptual
expert
3:00remaining
How does Angular's ngForm directive aggregate child controls' states?
In Angular template-driven forms, how does the ngForm directive determine the overall form's valid and dirty states?
AIt randomly picks one child control's state each time to set the form state.
BIt only checks the first child control's state to determine the form state.
CIt combines the states of all child controls; the form is valid only if all controls are valid, and dirty if any control is dirty.
DIt ignores child controls and always sets valid to true and dirty to false.
Attempts:
2 left
💡 Hint
Think about how a group behaves when one member changes or fails.