10

I am validating my template using angular2, but meanwhile it shows this 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>
    <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>
    </div>
  </div>
</form>

I dont know why it shows this error. Can anyone fix it?

1 Answer 1

24

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"
Sign up to request clarification or add additional context in comments.

2 Comments

Elvis operator worked for me. I was setting up my form groups in the constructor, which was apparently too late for the DOM.
@hholtij I couldn't post the full comment here, so I answered the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.