Consider this Angular template snippet for a simple form:
<form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)"> <input name="email" ngModel required email /> <button type="submit">Submit</button> </form>
What will be the state of myForm.valid if the input is empty and the user clicks Submit?
<form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)"> <input name="email" ngModel required email /> <button type="submit">Submit</button> </form>
Think about what the required attribute does in Angular forms.
The required attribute marks the input as mandatory. If empty, Angular sets myForm.valid to false, preventing submission.
In Angular template-driven forms, which attribute correctly enforces a minimum length of 5 characters on an input?
<input name="username" ngModel ... />Angular uses standard HTML5 validation attributes for length.
The correct attribute is minlength all lowercase. Angular recognizes it for validation.
Look at this input in an Angular template-driven form:
<input name="userEmail" ngModel required type="email" />
Despite entering invalid emails, the form shows userEmail.valid as true. What is the likely cause?
<input name="userEmail" ngModel required type="email" />
Angular template-driven forms need a specific attribute for email validation.
Angular requires the email attribute on inputs to apply its email validator. Just type="email" is not enough.
myForm.controls['password'].errors after input?Given this Angular template-driven form input:
<input name="password" ngModel required minlength="8" />
If the user types "abc" in the password field, what will myForm.controls['password'].errors contain?
<input name="password" ngModel required minlength="8" />
Think about what errors Angular reports when minlength is not met.
Angular sets the minlength error with details about required and actual lengths when input is too short.
In Angular, template-driven forms use validation attributes like required and minlength directly in the template. Reactive forms use validator functions in the component code.
What is a key advantage of using template attributes for validation?
Think about simplicity and where validation rules live.
Template attributes let you add validation rules directly in HTML, making it easier and faster to set up simple validations without extra code.