通过Angular中表单外部的按钮提交表单

时间:2018-10-30 08:58:44

标签: angular

如何通过位于表单外部的按钮以编程方式提交Angular(2+)表单? 应该通过单击事件来调用Angular组件中的函数吗?

2 个答案:

答案 0 :(得分:2)

我使用此处显示的方法(来自:How to trigger Angular 2 form submit from component?

  

NgForm具有属性ngSubmit,它是EventEmitter。所以在做上emit()   组件中的此属性将触发提交。

     

此外,您还需要使用f变量而不是formElement,因为f   正在引用ngForm。

     

@ViewChild('f')形式:NgForm;

     

form.ngSubmit.emit();

答案 1 :(得分:-1)

    **<formname>.ngSubmit.emit()** is used to submit the form when click the button from outside of the form.

For Example :    
    <form (ngSubmit)="save()" #submitForm="ngForm">
      <input type="text" name="name" required>
    </form>    
    <button (click)="submitForm.ngSubmit.emit()"
    [disabled]="!submitForm.form.valid">SAVE</button>

When click the SAVE button its call the save() function in the component.