简体   繁体   English

Angular提交外部表格

[英]Angular submit outside form

I need to invoke submit on form(s) when clicking on button outside the form as well as changing FormGroupDirective.submitted to true . 我需要在单击表单外部的按钮时调用submit上的submit以及将FormGroupDirective.submitted更改为true

I have next layout 我有下一个布局

<form (ngSubmit)="submit()" #myForm [formGroup]="formGroup">
...
</form>

<button (click)="formGroupDirective.ngSubmit.emit()" *ngIf="showEditControl"
    class="btn btn_blue mr-3 button-save" type="submit">
    {{'buttons.save' | translate}}
</button>

And component.ts 和component.ts

@ViewChild('myForm', {read: FormGroupDirective}) formGroupDirective: FormGroupDirective;

This does invoke (ngSumit) however the property submitted is not changed. 这会调用(ngSumit)但是submitted的属性不会更改。 It's not good to change the property itself as submitted is readonly . 改变属性本身并不好,因为submittedreadonly What could be done here (except for creating hidden submit buttons and emulating click on them)? 这里可以做什么(除了创建隐藏的提交按钮和模拟点击它们)?

use this. 用这个。

in html: 在HTML中:

<form [formGroup]="form" #myForm="ngForm">
    // ...Form Controls
</form>

<button (click)="submitForm()" *ngIf="showEditControl" class="btn btn_blue mr-3 button-save" type="submit">
    {{'buttons.save' | translate}}
</button>

and in component.ts 并在component.ts

@ViewChild('myForm') form: FormGroupDirective;

formGroup: FormGroup = new FormGroup({
    myInput: new FormControl(''),
    //etc...
});

submitForm() {
    this.form.onSubmit(undefined);
}

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

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