Angular - Submit a form programmatically

后端 未结 5 468
深忆病人
深忆病人 2021-02-07 02:40

Angular - Submit a form programmatically.

I have a form group on the HTML and I want the component to submit the form\'s action with an email field in a post method. Ins

相关标签:
5条回答
  • 2021-02-07 03:01

    You can use ngNoForm with your form to remove ngForm handling and to add plain javascript handler.

    you can use your code as follows:

    Html file.

    <form ngNoForm
      [formGroup]="testGroup"
      [action]='actionLink'
      method='POST'
      #testForm>
        <input name='Email' type='hidden' [value]='currentUserEmail'>
    </form>
    

    Ts File.

    @ViewChild('testForm') testFormElement;
    
    public currentUserEmail: string = '';
    public testGroup = this.formBuilder.group({
      Email: ''
    });
    
    constructor(formBuilder: FormBuilder) {
    }    
    
    public testMethod(): void {
      this.testFormElement.nativeElement.submit();
    }
    
    0 讨论(0)
  • 2021-02-07 03:01

    Regarding "Angular - Submit a form programmatically" Here is code where a form submits itself. It works for me. Hope this helps.

    Component HTML:

    <form #myForm ngNoForm name="myForm" [action]="pdfServletUrl" target="_blank" method="POST">
      <button type="submit" [hidden]="'true'"></button>
    </form>
    

    Component TypeScript:

    @ViewChild('myForm') myForm : ElementRef;
    
    ngAfterViewInit() {
      this.myForm.nativeElement.submit();
    }
    
    0 讨论(0)
  • 2021-02-07 03:07

    I think you should get ngForm in your code. So, rewrite your code as follows:

    <form
    [formGroup]="testGroup"
    [action]='actionLink'
    method='POST'
    #testForm="ngForm" (ngSubmit)="testForm.form.valid ? yourSaveMethod() :showValidatinErrors()">
      <input name='Email' type='hidden' [value]='currentUserEmail'>
    </form>
    

    and in your ts file:

    @ViewChild('testForm') testFormElement: NgForm;
    
    public testMethod(): void {
      // Below: This works for me.
      this.testFormElement.ngSubmit.emit();
    }
    
    public yourSaveMethod(): void {
      // post your model here.
    }
    
    0 讨论(0)
  • 2021-02-07 03:14
    import { Component, ViewChild } from '@angular/core';
    
    @Component({
        template: `
            <form #testForm>
                <input name='Email' type='hidden'>
            </form>
        `
    })
    class MyComponent {
        @ViewChild('testForm') testFormEl;
    
        testMethod() {
            this.testFormEl.nativeElement.submit()
        }
    }
    
    0 讨论(0)
  • 2021-02-07 03:23

    I have found a good solution on github

    it will setup submitted as true

    Out approach now is:

    HTML:

     <form [formGroup]="form" #myForm="ngForm" novalidate class="create-form">
    

    ts:

    @ViewChild('myForm') ngForm: NgForm;
    
    ...
    public onSubmit() {
     this.ngForm.onSubmit(undefined); // it will setup submitted as true
    
    0 讨论(0)
提交回复
热议问题