0
0
Angularframework~20 mins

ngModel for form binding in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ngModel Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output when using ngModel for two-way binding?
Consider this Angular component template snippet:
<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>
AHello, Alice!
BHello, {{ username }}!
CHello, !
DHello, undefined!
Attempts:
2 left
💡 Hint
ngModel updates the component property as you type.
📝 Syntax
intermediate
1:30remaining
Which option correctly uses ngModel for form binding?
Which of the following Angular template snippets correctly binds the 'email' property using ngModel?
A<input ngModel="email">
B<input [(ngModel)]="email">
C<input (ngModel)="email">
D<input [ngModel]="email">
Attempts:
2 left
💡 Hint
Two-way binding uses [( )] syntax.
🔧 Debug
advanced
2:00remaining
Why does this ngModel binding cause an error?
Given this Angular template:
<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;
}
ACannot read property 'name' of null
BngModel is not a known property
CNo error, works fine
DTemplate parse error: Unexpected token
Attempts:
2 left
💡 Hint
The user object is null when Angular tries to read 'name'.
state_output
advanced
2:00remaining
What is the value of 'age' after user input with ngModel?
In this Angular 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>
Aage becomes 0
Bage becomes an empty string ''
Cage remains 30
Dage becomes null
Attempts:
2 left
💡 Hint
For number inputs, empty input sets the bound property to null.
🧠 Conceptual
expert
2: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?
AThe input element ignores ngModel and works as a normal input
BngModel works but with warnings in the console
CAngular throws a template parse error: 'Can't bind to ngModel since it isn't a known property'
DThe app compiles but crashes at runtime
Attempts:
2 left
💡 Hint
ngModel directive is declared in FormsModule.