Vikram R Vikram R - 3 years ago 260
Javascript Question

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

I am getting

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

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




please tell me where is my mistake. thanks in advance.

Answer Source

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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download