Dimuthu
Dimuthu

Reputation: 409

angular 2 on form submit ERROR TypeError: _co.onSubmit is not a function

I am using angular 2 application. on form submit I am getting this error:

TypeError: _co.onSubmit is not a function

Here is a screenshot of the full error:

My form:

<form [formGroup]="userForm" (ngSubmit)="onSubmit()" >
<button class="btn btn-success" type="submit">Create User</button>

My userComponent.ts:

onSubmit(){
    console.log('clicked');
}

Upvotes: 2

Views: 2198

Answers (1)

viper
viper

Reputation: 39

I too have same error this error occurs when the name of the methods called in an event not matched with the template declaration and inside the class In my case the template was

<form class="container" #userRegistrationForm="ngForm" (ngSubmit)="OnSubmit(userRegistrationForm)">

and my component.ts file

       onSubmit(form:NgForm){
     this.userService.registerUser(form.value)
     .subscribe((data:any)=>{
       if(data.Succeeded==true)
       this.resetForm(form);
     });
   }

After changing onSubmit to OnSubmitin my component.ts file

I was able to resolve this error

Upvotes: 3

Related Questions