简体   繁体   English

Angular 响应式表单提交事件

[英]Angular Reactive Form Submission Event

I've built a validation component that works great.我构建了一个效果很好的验证组件。 However, I'd like to extend it by adding success messages when a form is submitted successfully.但是,我想通过在成功提交表单时添加成功消息来扩展它。

In the component I pass in the form, watch for changes, and act on the error:在我传入表单的组件中,观察变化,并对错误采取行动:

this.formGroup.valueChanges.pipe(takeUntil(this.ngUnsubscribe)).subscribe(data => {
    if (this.formGroup.invalid) {
        this.validation = { message: `Check for errors in the form`, valid: false };
    } else {
        this.validation = { message: '', valid: true };
    }
});

However I can't see a direct way to get a submission event into my component so that I can show success in the ui.但是,我看不到将提交事件放入我的组件以便我可以在 ui 中显示成功的直接方法。

  1. Can I use the (ngSubmit) event somehow?我可以以某种方式使用(ngSubmit)事件吗?
  2. what would you do to get it?你会怎么做才能得到它?

UPDATE:更新:

  1. After significant time invested into this my feature request is here在投入大量时间后,我的功能请求在这里

  2. Wrote about "success handling" on Medium 在 Medium 上写了关于“成功处理”的文章

  3. (2022) we finally have the ng-submitted class (v12.1) (2022) 我们终于有了ng-submitted class (v12.1)

I don't know any submission event directly from the FormGroup class, what I know is that you should listen it from the form template. 我不知道直接来自FormGroup类的任何提交事件,我知道您应该从表单模板监听它。

<form [formGroup]="formGroup" (ngSubmit)="submit()">
  <!-- Your controls here-->
  <button>Submit</button>
</form>

In your component code you should listen the submit event creating the submit() method 在您的组件代码中,您应该侦听提交事件,以创建Submit()方法

As of Angular 12.1 an ng-submitted class is added to the form element that has been submitted.从 Angular 12.1开始, ng-submitted class 添加到已提交的form元素中。

The class is automatically added on submissions, and removed on resets. class 在提交时自动添加,并在重置时删除。

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

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