Challenge - 5 Problems
ngModel Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What is the output when using ngModel for two-way binding?
Consider this Angular component template snippet:
If the user types 'Alice' in the input box, what will be displayed inside the paragraph?
<input [(ngModel)]="username" />
<p>Hello, {{ username }}!</p>
If the user types 'Alice' in the input box, what will be displayed inside the paragraph?
Angular
<input [(ngModel)]="username" />
<p>Hello, {{ username }}!</p>Attempts:
2 left
💡 Hint
ngModel updates the component property as you type.
✗ Incorrect
The [(ngModel)] syntax binds the input value to the 'username' property in both directions. When the user types 'Alice', 'username' updates immediately, so the paragraph shows 'Hello, Alice!'.
📝 Syntax
intermediate1:30remaining
Which option correctly uses ngModel for form binding?
Which of the following Angular template snippets correctly binds the 'email' property using ngModel?
Attempts:
2 left
💡 Hint
Two-way binding uses [( )] syntax.
✗ Incorrect
Option B uses the correct two-way binding syntax [(ngModel)]="email". Option B is one-way property binding, C is invalid event binding, and A is invalid attribute usage.
🔧 Debug
advanced2:00remaining
Why does this ngModel binding cause an error?
Given this Angular template:
and the component class:
What error will occur when the template tries to bind?
<input [(ngModel)]="user.name" />
and the component class:
export class MyComponent {
user = null;
}What error will occur when the template tries to bind?
Angular
<input [(ngModel)]="user.name" /> export class MyComponent { user = null; }
Attempts:
2 left
💡 Hint
The user object is null when Angular tries to read 'name'.
✗ Incorrect
Since 'user' is null, accessing 'user.name' causes a runtime error: Cannot read property 'name' of null. The template tries to bind to a property of a null object.
❓ state_output
advanced2:00remaining
What is the value of 'age' after user input with ngModel?
In this Angular component:
and template:
If the user deletes the input content making it empty, what will be the value of 'age' in the component?
age = 30;
and template:
<input type="number" [(ngModel)]="age" />
<p>Age: {{ age }}</p>
If the user deletes the input content making it empty, what will be the value of 'age' in the component?
Angular
age = 30; <input type="number" [(ngModel)]="age" /> <p>Age: {{ age }}</p>
Attempts:
2 left
💡 Hint
For number inputs, empty input sets the bound property to null.
✗ Incorrect
When the input type='number' is cleared, ngModel's NumberValueAccessor sets 'age' to null. It does not set an empty string, remain 30, or become 0.
🧠 Conceptual
expert2:30remaining
Why must FormsModule be imported to use ngModel?
In Angular, if you use [(ngModel)] in a component template without importing FormsModule in your module, what will happen?
Attempts:
2 left
💡 Hint
ngModel directive is declared in FormsModule.
✗ Incorrect
ngModel is a directive provided by FormsModule. Without importing FormsModule, Angular does not recognize ngModel and throws a template parse error.