Bird
Raised Fist0
Angularframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the ngForm directive do in Angular?
easy
A. It tracks the state and validity of a form and its inputs.
B. It styles the form elements automatically.
C. It replaces the need for HTML form tags.
D. It sends form data directly to the server.

Solution

  1. Step 1: Understand the role of ngForm

    The ngForm directive is used to track the state of a form and its inputs in Angular.
  2. Step 2: Identify what ngForm does not do

    It does not style elements, replace HTML tags, or send data directly; it manages form state and validation.
  3. Final Answer:

    It tracks the state and validity of a form and its inputs. -> Option A
  4. Quick Check:

    ngForm tracks form state = D [OK]
Hint: Remember: ngForm manages form state, not styling or data sending [OK]
Common Mistakes:
  • Thinking ngForm styles the form
  • Assuming ngForm replaces HTML form tags
  • Believing ngForm sends data automatically
2. Which of the following is the correct way to create a local reference to a form using ngForm in Angular?
easy
A. ...
B. ...
C. ...
D. ...

Solution

  1. Step 1: Recall the syntax for local form reference

    To access the form's state, Angular uses a template reference variable with ngForm as its value, like #myForm="ngForm".
  2. Step 2: Check each option's correctness

    ... correctly uses #myForm="ngForm". ... misses the directive name, C misplaces the directive, and D uses incorrect binding syntax.
  3. Final Answer:

    <form #myForm="ngForm"> ... </form> -> Option B
  4. Quick Check:

    Local form reference = #myForm="ngForm" [OK]
Hint: Use #var="ngForm" to access form state locally [OK]
Common Mistakes:
  • Omitting the "ngForm" in the reference
  • Using square brackets incorrectly
  • Placing ngForm attribute wrongly
3. Given this Angular template snippet:
<form #f="ngForm">
  <input name="email" ngModel required>
  <button [disabled]="!f.form.valid">Submit</button>
</form>

What happens to the Submit button when the input is empty?
medium
A. The Submit button is enabled regardless of input.
B. The Submit button is disabled only if the input is touched.
C. The Submit button is disabled because the form is invalid.
D. The Submit button is enabled only if the input is dirty.

Solution

  1. Step 1: Understand form validity with required input

    The input has the required attribute, so if empty, the form is invalid.
  2. Step 2: Analyze the button's disabled binding

    The button is disabled when !f.form.valid is true, meaning when the form is invalid.
  3. Final Answer:

    The Submit button is disabled because the form is invalid. -> Option C
  4. Quick Check:

    Required empty input disables button = B [OK]
Hint: Button disabled if form invalid (required empty input) [OK]
Common Mistakes:
  • Thinking button enables on untouched input
  • Confusing dirty and valid states
  • Ignoring required validation effect
4. Identify the error in this Angular form snippet:
<form #myForm="ngForm">
  <input name="username" ngModel>
  <button [disabled]="myForm.invalid">Submit</button>
</form>

Why does the Submit button remain enabled even when the input is empty?
medium
A. The button binding should use myForm.form.invalid instead of myForm.invalid.
B. The local reference #myForm is incorrectly declared.
C. The ngModel directive is missing on the input.
D. The input lacks a validation rule like required, so form is valid.

Solution

  1. Step 1: Check input validation rules

    The input has no validation like required, so it does not make the form invalid when empty.
  2. Step 2: Confirm form validity property usage

    myForm.invalid is valid shorthand for myForm.form.invalid, so no error there.
  3. Final Answer:

    The input lacks a validation rule like required, so form is valid. -> Option D
  4. Quick Check:

    Missing required means form valid = C [OK]
Hint: Add required to input to make form invalid when empty [OK]
Common Mistakes:
  • Assuming myForm.invalid is incorrect syntax
  • Forgetting to add validation rules
  • Thinking ngModel is missing when it is present
5. You want to disable the Submit button only after the user has changed any input in the form and the form is invalid. Which condition correctly uses ngForm states to achieve this?
hard
A. [disabled]="myForm.form.invalid && myForm.form.dirty"
B. [disabled]="myForm.invalid || myForm.pristine"
C. [disabled]="!myForm.form.valid && myForm.touched"
D. [disabled]="myForm.form.valid && myForm.dirty"

Solution

  1. Step 1: Understand form state properties

    myForm.form.invalid is true if form is invalid; myForm.form.dirty is true if user changed any input.
  2. Step 2: Match condition to requirement

    We want to disable the button only if form is invalid AND user has changed input, so both must be true.
  3. Final Answer:

    [disabled]="myForm.form.invalid && myForm.form.dirty" -> Option A
  4. Quick Check:

    Disable if invalid and dirty = A [OK]
Hint: Use invalid AND dirty to disable after user changes invalid form [OK]
Common Mistakes:
  • Using OR instead of AND for disabling
  • Confusing touched with dirty
  • Checking valid instead of invalid
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard