Complete the code to create a template-driven form using ngForm.
<form #myForm="[1]"> <input name="username" ngModel> </form>
The ngForm directive is used to create a template-driven form in Angular. It tracks the form state and allows access to form properties.
Complete the code to check if the form is valid using ngForm.
<form #myForm="ngForm"> <button [disabled]="[1]">Submit</button> </form>
The invalid property of ngForm is true when the form has invalid controls. Disabling the button when the form is invalid prevents submission.
Fix the error in the code to correctly bind the form reference using ngForm.
<form [1]> <input name="email" ngModel> </form> <p *ngIf="formRef.dirty">Form is dirty</p>
To get a reference to the form using ngForm, use #formRef="ngForm". This assigns the form directive instance to the template variable formRef.
Fill both blanks to create a form that disables the submit button when the form is invalid or untouched.
<form #myForm="ngForm"> <button [disabled]="myForm.[1] || myForm.[2]">Submit</button> </form>
The button should be disabled if the form is invalid or pristine (not changed yet). This prevents submitting an untouched or invalid form.
Fill all three blanks to create a form that shows a message when the form is submitted and valid.
<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>
The form calls onSubmit() on submit. The button disables if the form is invalid. The success message shows if submitted is true and the form is valid.