1

Cannot read property 'error' of undefined in Angular 2?

During use of following code, I have code following error ,I think I have to import some proerty like FormModule in module.ts, but I don't not what is that?? Please help me to solve this?

Firstname: <input type="text" class="_firstname" name="txtFirstname"    [(ngModel)]="firstName" placeholder="Enter First Name"  #txtFather="ngModel"><br><br>
<div *ngIf="txtFirstname.error &&(txtFirstname.dirty || txtFirstname.touched)" class="alert alert-danger">
    <div [hidden]="!txtFirstname.errors.required">
          Name  is required!
   </div>
</div>

I've got:

reg.component.html:8 ERROR TypeError: Cannot read property 'error' of undefined
    at Object.eval [as updateDirectives] (reg.component.html:9)
    at Object.debugUpdateDirectives [as updateDirectives] (services.ts:443)
    at checkAndUpdateView (view.ts:359)
    at callViewAction (view.ts:767)
    at execComponentViewsAction (view.ts:700)
    at checkAndUpdateView (view.ts:413)
    at callViewAction (view.ts:767)
    at execComponentViewsAction (view.ts:700)
    at checkAndUpdateView (view.ts:413)
    at callWithDebugContext (services.ts:815)
1
  • 1
    Well, your control variable is txtFather: #txtFather="ngModel" Commented Jul 17, 2018 at 6:20

4 Answers 4

4

You haven't an element with name txtFirstname. You have used #txtFather="ngModel", so you need to use txtFather.error instead of txtFirstname.error.

You have bind your input to the property firstName, but your control name is txtFather.

<input type="text" class="_firstname" name="txtFirstname" [(ngModel)]="firstName" placeholder="Enter First Name"  #txtFather="ngModel">
<br><br>
<div *ngIf="txtFather.error &&(txtFather.dirty || txtFather.touched)" class="alert alert-danger">
   <div [hidden]="!txtFather.errors.required">
          Name  is required!
   </div>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

#Suran ,But Still its not working, Is there Any error
@Suran, thanks For Help,{ Actually I missed 'required' in first line}
1

Use ? to check null / undefined values like this,

txtFirstname?.error 

Hope this helps!

Comments

1

You need to state following in the top area of your module.ts

import { FormsModule }   from '@angular/forms';

And after that you need to put it in the "imports" brackets of the @NgModule Part

This is an example from the Angular Documentation:

@NgModule({


imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent,
    HeroFormComponent
  ],
  providers: [],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

Comments

0

This error is given because txtFirstname is undefined. It seems that you don't have such variable.

Comments

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.