简体   繁体   English

在angular中调用表单外的表单提交按钮

[英]Calling the submit button of the form outside the form in angular

I have a form in angular which I use to add and edit the record.我在 angular 中有一个表格,用于添加和编辑记录。 I want this submit button outside the form.我想要表单外的这个提交按钮。

I am using the mat dialog and want the submit button in the mat-dialog actions tag.我正在使用 mat 对话框并希望在 mat-dialog 操作标记中使用提交按钮。 The submit button that I place in the actions part of the dialog is not making the form to submit.我在对话框的操作部分放置的提交按钮没有使表单提交。

<h2 mat-dialog-title>
  <h1 class="pb-4" style="padding-left: 50px">
    <span *ngIf="!userId">Add new</span><span *ngIf="userId">Update</span> user
  </h1>
</h2>

<mat-dialog-content class="mat-typography">
  <div class="dialog-padding">
    <ng-container *ngIf="isLoading$ | async">
      <div class="overlay-layer bg-transparent">
        <div class="spinner spinner-lg spinner-success"></div>
      </div>
    </ng-container>
    <div>
      <form
        #myform="ngForm"
        class="form"
        id="kt_form"
        (ngSubmit)="save(myform)"
      >
        <div class="panel panel-primary">
          <div class="panel-body">
            <div class="row">
              <div class="col-xl-12">
                <div class="form-group">
                  <label class="control-label" for="name">Name</label>
                  <input
                    required
                    type="text"
                    [(ngModel)]="model.name"
                    class="form-control form-control-solid"
                    placeholder=""
                    name="name"
                    #name="ngModel"
                  /> 
                </div>
              </div>
            </div>

            <div class="row pt-4" style="border-top: 1px solid #f5f5f5">
              <div class="col-xl-6"></div>
              <div class="col-xl-6">
                <div class="form-group">
                  <button class="btn btn-primary width-100">
                    {{ neworUpdate }} user
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</mat-dialog-content>

<mat-dialog-actions align="end">
  <button
    mat-button
    [mat-dialog-close]="true"
    cdkFocusInitial
    class="btn btn-primary btn-pointer"
  >
    Install
  </button>

  <button mat-button mat-dialog-close class="btn btn-primary btn-pointer">
    Cancel
  </button>
</mat-dialog-actions>

Assign an Id to your form so you'll be able to put the submit button wherever you want by specifying button's type as submit and the form it is addressing to.为您的表单分配一个 Id,这样您就可以通过将按钮的类型指定为submit以及它要寻址的表单来将提交按钮放在您想要的任何位置。 Something like this:是这样的:

<form 
   id="myform" <=====
   #myform="ngForm" 
   class="form" 
   ...>
     ....
</form>

Now, you can place the button anywhere on the template, not necessarily inside the form tags:现在,您可以将按钮放在模板的任何位置,不一定在表单标签内:

<button 
  mat-button 
  type="submit" <====
  form="myform" <====
  [mat-dialog-close]="true"
  cdkFocusInitial
  class="btn btn-primary btn-pointer">
     Install
</button>

If you want to use submit button for your form, you should add your button between tags for example (from docs):如果你想为你的表单使用提交按钮,你应该在标签之间添加你的按钮,例如(来自文档):

<form [formGroup]="checkoutForm" (ngSubmit)="onSubmit()">
<div>
    <label for="name">
      Name
    </label>
    <input id="name" type="text" formControlName="name">
  </div>

<button class="button" type="submit">Purchase</button>

</form>

Actually I am using the mat dialog and want the submit button in the mat-dialog actions tag.实际上我正在使用 mat 对话框并希望在 mat-dialog 操作标签中使用提交按钮。

In documentation( https://angular.io/start/start-forms ) it says:在文档( https://angular.io/start/start-forms )中它说:

On the form tag, use an ngSubmit event binding to listen for the form submission在表单标签上,使用 ngSubmit 事件绑定来监听表单提交

Since you are outside of form tags, you should modify your button like this:由于您在表单标签之外,因此您应该像这样修改按钮:

<button
mat-button
[mat-dialog-close]="true"
cdkFocusInitial
class="btn btn-primary btn-pointer"
(click)="save()"
>
Install
</button>

Or you can try this answer: Angular2 - Validate and submit form from outside或者您可以试试这个答案: Angular2 - 从外部验证和提交表单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM