2017-04-07 57 views
2

我遵循角2教程,並試圖以角2形式進行驗證。見代碼:角度2表單驗證給出未定義的錯誤消息

<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> 

的問題是,我收到此錯誤代碼:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'errors' of undefined 

我試圖實現它完全按照官方網站上的教程:https://angular.io/docs/ts/latest/cookbook/form-validation.html#!#template1

+3

把'#name =「ngModel」'改成'#email =「ngModel」' –

+1

謝謝喬治,就是這樣。提出一個答案,以便我可以upvote你並將其標記爲正確的答案。 – ionescho

+1

不需要。我很高興評論幫助 –

回答

2

答案(感謝喬治K)是我不小心寫了#name="ngModel"而不是#email="ngModel"