nlr_p nlr_p - 1 year ago 322
HTML Question

Cannot read property 'errors' of undefined in angular 2

I am validating my template using angular2 but mean while it shows the error

Cannot read the property 'errors' of undefined.

Here is my template.......

<h3 class = "head">{{title}}</h3>
<form [ngFormModel]="form" #f="ngForm">
<div class="row">
<div class="form-group">
<label class="formHeading">Facebook</label>
<input type="text" id="facebook" class="form-control col-xs-3" ngControl="facebook" #facebook="ngForm" >
<div *ngIf ="facebook.touched && facebok.errors">
<div class="form-row btn">
<button type="submit" class="btn btn-primary pull-right butspace" [disabled]="!f.valid">Save</button>

I dont know why it shows error,can anyone fix it

Answer Source

First of all, you have facebok instead of facebook on the error property.

If that doesn't fix it you're probably using the facebook object before it is assigned, which can happen if it's an @Input() for example.

Use the Elvis operator:

*ngIf ="facebook?.touched && facebook?.errors"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download