Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR TypeError: Cannot read property 'invalid' of undefined angular 4

Tags:

I am getting the following error in Angular 4 :

ERROR TypeError: Cannot read property 'invalid' of undefined

Here's the code :

<form name="form"  #f="ngForm" novalidate>

  <tr>
     <td width="10%" class="text-blue"></td> 
     <td><input type="text"  name="bankname" maxlength="25" appRestrictinput ="words" [(ngModel)]="model.bk_name" #bk_name="ngModel"  class="form-control" autofocus  /> </td>
  </tr>
  <div *ngIf="bankname.invalid && (bankname.dirty || bankname.touched)" class="alert alert-danger">
     <div *ngIf="bankname.errors.required">Name is required</div>
  </div>

</form>

Please let me know where I'm going wrong. Thanks in advance.

like image 827
Vikram R Avatar asked Oct 01 '17 07:10

Vikram R


2 Answers

Your control reference in the view is bk_name. Replace bankname with bk_name.

<div *ngIf="bk_name.invalid && (bk_name.dirty || bk_name.touched)" class="alert alert-danger">
<div *ngIf="bk_name.errors.required">Name is required.</div>
like image 54
Suren Srapyan Avatar answered Sep 22 '22 13:09

Suren Srapyan


You Just forgot about Template variable

Add this to your input #bankname="ngModel" and it will work with you

<input ngModel name="bankname" type="text" #bankname="ngModel" class="form-control" placeholder="Name" required> <br/>

<div class="alert alert-danger" *ngIf="bankname.invalid">bankname is required</div>
like image 27
Dariwsh D Avatar answered Sep 20 '22 13:09

Dariwsh D