I followed the angular 2 tutorial and tried to implement validation in a angular 2 form. See code:
<form #loginForm="ngForm" (ngSubmit)="login()">
<div>
<label for="email">Email:</label>
<input type="text" id="email" name="email" required minlength="4" maxlength="24" [(ngModel)]="emailField" #name="ngModel" />
<div *ngIf="email?.errors && (email?.dirty || email?.touched)" class="alert alert-danger">
<div [hidden]="!email.errors.required">
email is required
</div>
<div [hidden]="!email.errors.minlength">
email must be at least 4 characters long.
</div>
<div [hidden]="!email.errors.maxlength">
Name cannot be more than 24 characters long.
</div>
</div>
</div>
<div>
<label>Password:</label><input type="password" name="password" [(ngModel)]="passwordField" />
</div>
<div>
<button [disabled]="!loginForm.form.valid" type="submit">Submit</button>
</div>
</form>
The problem is that I am getting this error code :
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'errors' of undefined
I tried to implement it exactly as in the tutorial on the official site: https://angular.io/docs/ts/latest/cookbook/form-validation.html#!#template1
#name="ngModel"to#email="ngModel"