0
0
Angularframework~10 mins

ngForm directive and form state in Angular - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a template-driven form using ngForm.

Angular
<form #myForm="[1]">
  <input name="username" ngModel>
</form>
Drag options to blanks, or click blank then click option'
AngForm
BformGroup
CformControl
DformControlName
Attempts:
3 left
💡 Hint
Common Mistakes
Using formGroup instead of ngForm in template-driven forms.
Forgetting to add ngModel on input fields.
2fill in blank
medium

Complete the code to check if the form is valid using ngForm.

Angular
<form #myForm="ngForm">
  <button [disabled]="[1]">Submit</button>
</form>
Drag options to blanks, or click blank then click option'
AmyForm.invalid
BmyForm.valid
CmyForm.pristine
DmyForm.touched
Attempts:
3 left
💡 Hint
Common Mistakes
Using myForm.valid to disable the button (this disables when valid, which is wrong).
Confusing pristine and touched with validity.
3fill in blank
hard

Fix the error in the code to correctly bind the form reference using ngForm.

Angular
<form [1]>
  <input name="email" ngModel>
</form>
<p *ngIf="formRef.dirty">Form is dirty</p>
Drag options to blanks, or click blank then click option'
A#ngForm
B#formRef="ngForm"
CformRef="ngForm"
D#formRef
Attempts:
3 left
💡 Hint
Common Mistakes
Using #ngForm instead of #formRef="ngForm".
Trying to bind formRef="ngForm" which is invalid syntax.
4fill in blank
hard

Fill both blanks to create a form that disables the submit button when the form is invalid or untouched.

Angular
<form #myForm="ngForm">
  <button [disabled]="myForm.[1] || myForm.[2]">Submit</button>
</form>
Drag options to blanks, or click blank then click option'
Ainvalid
Bpristine
Ctouched
Ddirty
Attempts:
3 left
💡 Hint
Common Mistakes
Using touched instead of pristine to check if form is untouched.
Using dirty instead of pristine.
5fill in blank
hard

Fill all three blanks to create a form that shows a message when the form is submitted and valid.

Angular
<form #myForm="ngForm" (ngSubmit)="[1]()">
  <input name="name" ngModel required>
  <button type="submit" [disabled]="myForm.[2]">Submit</button>
</form>
<p *ngIf="submitted && myForm.[3]">Form submitted successfully!</p>
Drag options to blanks, or click blank then click option'
AonSubmit
Binvalid
Cvalid
Dpristine
Attempts:
3 left
💡 Hint
Common Mistakes
Using pristine instead of invalid to disable the button.
Showing success message when form is invalid.