2

my Submit button is outside of the form and i have no idea how to emit the submit event to the form. I tried the following:

 @ViewChild('myForm') myForm: any; // binding to my form

  clickSubmitButton(): void {
    this.myForm.submit(); 
  }

I get the following error: this.myForm.submit is not a function.

5
  • Please post the exact error message. Commented Sep 9, 2016 at 14:15
  • What is submit() supposed to do anyway? Commented Sep 9, 2016 at 14:17
  • After i press the submit button in the form, it should show the validation messages for my inputs. Commented Sep 9, 2016 at 14:31
  • Angular2 valdiation messages are shown immediately and not only when the form is submitted. Therefore I still have troubles to understand what you actually try to accomplish. Commented Sep 9, 2016 at 14:34
  • Please show that template. Commented Sep 9, 2016 at 14:36

2 Answers 2

2

This should work:

@ViewChild('myForm', {read: NgForm} ) myForm: any; 

and

clickSubmitButton(): void {
  this.myForm.onSubmit(); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

hmm its not working for me. i get again the following error: this.myForm.onSubmit is not a function
0

You can use ElementRef

@ViewChild('myForm') myForm: ElementRef;

then

this.myForm.nativeElement.submit();

Don't forget import ElementRef

import { ElementRef } from '@angular/core';

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.